MongoDB is a NoSQL database that holds data records as
Here are the data types that we can use in MongoDB.
The BSON strings are UTF-8. The programming language drivers will convert the string type to UTF-8 during the serialization and de-serialization of the BSON.
Here’s an example:
name: "Jhon"
The float values are stored in the double data type.
Here’s an example:
marks: 97.6
The integer values are stored in the integer data type in MongoDB.
Here’s an example:
age: 22
In MongoDB, the array is represented by the square brackets []
. The array can have multiple values of the same or different data types.
Here’s an example:
subjects: [ "English", "Social Studies", "Economics" ],
To store the nested documents, we use the object data type.
Here’s an example:
address: {city: "New York", country: "USA"}
This binary data type is used to store the binary values.
Here’s an example:
bin_ata: 101010
Every document in the Mongo DB will have an _id
attribute. This attribute is a unique identifier for a document and will be of type Objectid
. Mongo DB creates this by default for every document, but users can specify it explicitly.
Here’s an example:
_id: ObjectId("5099803df3f4948bd2f98391")
Either true
or false
is stored in the boolean data type.
Here’s an example:
pass: true
The null values are stored in the null
data type.
Here’s an example:
lastname: null
The date
data type in MongoDB is used to store the date. We can specify a custom date by passing day, month, and year to the date object.
Here’s an example:
birth: new Date('Jun 23, 2000')
The regular expressions can be stored in MongoDB with BSONRegExp
data type.
Here’s an example:
RegularExpression: new RegExp("%Subject%"),
This data type is used to store a timestamp
. Timestamps in the documents are usually used for auditing purposes. This is always a 64-bit value.
Here’s an example:
timestamp: new Timestamp()
This data type is used to store 128-bit decimal values.
Here’s an example:
average: NumberDecimal("95.8967")
In MongoDB, you can also store JavaScript functions without scope.
For example,
jscode: function(){var x; x=1}
Now we know the data types in MongoDB, let’s insert a document containing all the data types.
Note: We can try the following commands by creating an
and account https://www.mongodb.com/cloud/atlas/register to MongoDB connecting https://www.mongodb.com/docs/atlas/connect-to-database-deployment/#connect-to-a-cluster or to CLI. We'll insert a document via CLI and check it in compass for this example. compass https://www.mongodb.com/products/tools/compass
The following object is created from the examples discussed above:
Atlas atlas-ol25pb-shard-0 [primary] blogdb> var mydoc = {_id: ObjectId("5099803df3f4948bd2f98391"),name: "Jhon",lastname: null,birth: new Date('Jun 23, 2000'),subjects: ["English", "Social Studies", "Economics"],marks: 97.6,age: 22,address: { city: "New York", country: "USA" },bin_ata: BinData(0, 'AQAAAAEBAAVlbl9VSwAAAAAAAAhv'),pass: true,regular_expression: new RegExp("%Subject%"),timestamp: new Timestamp(),average: NumberDecimal("95.8967"),jscode: function () {var score;return score += 1;}}
We'll insert the document in this example:
Atlas atlas-ol25pb-shard-0 [primary] blogdb> db.inserts.insert(mydoc){acknowledged: true,insertedIds: { '0': ObjectId("5099803df3f4948bd2f98391") }}
Now, let’s check the data types in the compass.
use test; db.createCollection("inserts") var mydoc = { _id: ObjectId("5099803df3f4948bd2f98391"), name: "Jhon", lastname: null, birth: new Date('Jun 23, 2000'), subjects: ["English", "Social Studies", "Economics"], marks: 97.6, age: 22, address: { city: "New York", country: "USA" }, bin_ata: BinData(0, 'AQAAAAEBAAVlbl9VSwAAAAAAAAhv'), pass: true, regular_expression: new RegExp("%Subject%"), timestamp: new Timestamp(), average: NumberDecimal("95.8967"), jscode: function () {var score;return score += 1;} } db.inserts.insert(mydoc)
Note: We have already created a collection named "inserts" and have inserted a document in it using the above file named "task".
Click the run button to start mongoDB compass.
After skipping information panel you will see a connect button.
Just click on connect button and your database will be connected.
After that choose the database named as "test".
Choose the document named "inserts" and click on edit then you will see the following screen.
Free Resources