What are the data types in MongoDB?

MongoDB is a NoSQL database that holds data records as BSONhttps://bsonspec.org documents. BSON stands for Binary JSON, i.e., BSON is a binary representation of JSON documents. The MongoDB documents follow the structure of field and value pairs.

Data types in MongoDB

Here are the data types that we can use in MongoDB.

String

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"

Double

The float values are stored in the double data type.

Here’s an example:

marks: 97.6

Integer

The integer values are stored in the integer data type in MongoDB.

Here’s an example:

age: 22

Array

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" ],

Object

To store the nested documents, we use the object data type.

Here’s an example:

address: {city: "New York", country: "USA"}

Binary data

This binary data type is used to store the binary values.

Here’s an example:

bin_ata: 101010

ObjectId

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")

Boolean

Either true or false is stored in the boolean data type.

Here’s an example:

pass: true

Null

The null values are stored in the null data type.

Here’s an example:

lastname: null

Date

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')

BSONRegExp

The regular expressions can be stored in MongoDB with BSONRegExp data type.

Here’s an example:

RegularExpression: new RegExp("%Subject%"),

Timestamp

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()

Decimal128

This data type is used to store 128-bit decimal values.

Here’s an example:

average: NumberDecimal("95.8967")

Code

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 accounthttps://www.mongodb.com/cloud/atlas/register and connectinghttps://www.mongodb.com/docs/atlas/connect-to-database-deployment/#connect-to-a-cluster to MongoDB compasshttps://www.mongodb.com/products/tools/compass or to CLI. We'll insert a document via CLI and check it in compass for this example.

Create a document and store it in a variable

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;}
}

Insert the document

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") }
}

Using MongoDB Compass

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".

Steps to follow:

  • 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.

widget

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved