What are the Data Types in MongoDB?
Introduction
If you’re working with MongoDB or thinking about using it for your next project or product, one of the first basic questions you’ll ask yourself is “What data types can I store using MongoDB?”. In this article we’ll discuss the available data types and explain the more commonly used ones with enough detail that you can start getting your hands dirty and understand how you might want to start storing your data.
Prerequisites
There are no real prerequisites for this article but if you want to try storing some documents with these data types, and we recommend you do, you’ll need to have the following:
- You should have MongoDB installed and running
- You can start MongoDB with the
mongod
command once you have MongoDB installed
- You can start MongoDB with the
- You should have access to the MongoDB command line
- You can get access to the command line be executing the
mongo
command while MongoDB is running.
- You can get access to the command line be executing the
Basic Data Types
We’ll try to go in order of what we believe are the most commonly used data types, so let’s get started.
Remember that documents in MongoDB are stored in a JSON structure so an example of a document might be
1 | { "field": "value"} |
String Data Type
You can store strings with in MongoDB like so:
1 | { "name": "Thomas"} |
Array Data
You can store an array of data in MongoDB like so:
1 | { "groceryList": ["apples", "milk", "rice"]} |
In this example we stored three strings in the array but we the data types within the array don’t have to be the same. Let’s see an example of an array with different data types:
1 | { "randomList": ["string", 3.2, "anotherString"]} |
As you can see we can mix data types in an array and there is no special syntax necessary to do so.
Boolean
You can store boolean ( true or false ) values in MongoDB like so:
1 | {"isReady": true} |
Numbers
To store numbers we have different options depending on whether the number you intend to store needs how large the number is, if it contains decimals, and if so how much precision it needs. If you are only working through the MongoDB shell, be aware that the shell treats all numbers as floating point and you need to explicitly declare to use the other types.
32-bit Int
This type is used for whole numbers like so:
1 | {"int32": NumberInt(100)} |
64-bit Int
This type is used for very large whole numbers like so:
1 | {"int32": NumberLong(1188123456789)} |
$ 64-bit Floating Point
For decimals you’ll use the 64-bit floating point data type. This is the default type in the MongoDB shell, so if you don’t specify the type it will be stored as a 64-bit floating point. Let’s take a look at an example:
1 | {"price": 3.50} |
Object Ids
An Object Id is a number that is used to refer to another document. Every document has an Object Id (“_id”) that identifies it and these Object Ids are used to relate data together.
You’ll often see a documents id like this:
1 2 3 4 | { "_id" : ObjectId("123412341234123412341234"), ... } |
And a document that is connected do another document might look like this:
1 2 3 4 5 | { "_id" : ObjectId("123412341234123412341234"), "user_id" : ObjectId("123412341234123412345555"), "moreData": "data data data", } |
This is linking to another user document and could be used to easily join the sets of data together.
Date
Of course you’ll need some way to keep dates and timestamps in your database so MongoDB has the Date data type which is stored in epoch time. You can use it like so:
1 | {"updated" : new Date()} |
Binary
Binary is the data type you’d use to store information such as an image file. You should be aware though that documents shouldn’t be bigger than 16MB so if you’re storing this type of data this should be on your radar as a potential problem. There are solutions out there though if your data is larger than 16MB and will allow you to split up your data into smaller pieces and store those.
Code
This one is a little surprising but has some great applications. You can store straight javascript code in your database.
1 | {"jsFunction" : function(x) { return x; } } |
Undefined
Like javascript you can have undefined as a value:
1 | {"field" : undefined} |
Null
Like javascript, MongoDB also has the null value (different than the undefined value) and can be utilized like so:
1 | {"field" : null} |
Embedded Documents
It’s important to note that each field is not limited to containing one value but can have another document inside it:
1 | {"user" : {"firstname" : "Thomas", "lastname": "Edison"}} |
And you can keep nesting documents. This type of nesting can may seem a little complicated but keeps your data structure more organized for example if all the user information lives inside one field.
Regex
This is not a widely used application but you can store regular expressions or regex patterns in MongoDB. The syntax should be familiar if you’ve dealt with regular expressions before.
{“regexPattern” : /pattern/}
Conclusion
We hope you found this article on the basic data types within MongoDB helpful and that it will guide you in architecting whatever project or product you are building. Remember that you can mix and match these data types within arrays and in embedded objects so don’t feel limited. Once you start joining data from different collections you’ll want to start using Object Ids. Thank you for joining us and if Object Rocket can answer any of your questions or can assist you in setting up your database please don’t hesitate to reach out and ask.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started