MongoDB Data Types(BSON)

MongoDB stores the documents as BSON documents. BSON means, it is encoded binary format of JSON. BSON is providing the many data types to store the javascript objects into mongo collection. MongoDB supports the all type of BSON data types.
You can get the list of BSON data types Which is supported by mongo db.

Array

Array is a data structure and It is used to store the group of elements. We can store different type of values into array.

> db.BSON_Collection.insert({"array data type": [100, 200, 300, 400]})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.insert({"array data type": ["MongoDB", "Redis", "Mysql", "Oracle"]})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.insert({"array data type": [121.55, "NewYork - USA", 58695]})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.find().pretty();
{
        "_id" : ObjectId("5ecce3fcb8d2c3f9f691ab26"),
        "array data type" : [
                100,
                200,
                300,
                400
        ]
}
{
        "_id" : ObjectId("5ecce402b8d2c3f9f691ab27"),
        "array data type" : [
                "MongoDB",
                "Redis",
                "Mysql",
                "Oracle"
        ]
}
{
        "_id" : ObjectId("5ecce406b8d2c3f9f691ab28"),
        "array data type" : [
                121.55,
                "NewYork - USA",
                58695
        ]
}
>

Binary

Binary data type is used for storing the binary data in it.

Boolean

Boolean data type is used for storing boolean values true or false.

> db.BSON_Collection.insert({"boolean data type": true})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.insert({"boolean data type": false})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.find()
{ "_id" : ObjectId("5eccc378b8d2c3f9f691ab03"), "boolean data type" : true }
{ "_id" : ObjectId("5eccc380b8d2c3f9f691ab04"), "boolean data type" : false }
>

Code

This datatype is used to store JavaScript code into the document.

Date

Date is used for storing the date and time value.

> db.BSON_Collection.insert({"Date data type": Date()})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.insert({"Date data type": new Date()})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.insert({"Date data type": new Date("2020-05-25T20:00:00Z")})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.insert({"Date data type": new Date("2020-05-25")})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.insert({"Date data type": new ISODate()})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.find().pretty()
{
        "_id" : ObjectId("5eccc80ab8d2c3f9f691ab16"),
        "Date data type" : "Tue May 26 2020 13:10:58 GMT+0530 (India Standard Time)"
}
{
        "_id" : ObjectId("5eccc80fb8d2c3f9f691ab17"),
        "Date data type" : ISODate("2020-05-26T07:41:03.012Z")
}
{
        "_id" : ObjectId("5eccc813b8d2c3f9f691ab18"),
        "Date data type" : ISODate("2020-05-25T20:00:00Z")
}
{
        "_id" : ObjectId("5eccc818b8d2c3f9f691ab19"),
        "Date data type" : ISODate("2020-05-23T00:00:00Z")
}
{
        "_id" : ObjectId("5eccce7ab8d2c3f9f691ab20"),
        "Date data type" : ISODate("2020-05-23T08:08:26.049Z")
}
>


Double

Double data type is used for recording floating point values.

> db.BSON_Collection.insert({"double data type": 1.23456789})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.find()
{ "_id" : ObjectId("5eccbfc9b8d2c3f9f691aaff"), "double data type" : 1.23456789 }
>


Integer

The maximum value of Int32 is 2147483647.
The maximum value of Int64 is 9,223,372,036,854,775,807

> db.BSON_Collection.insert({"integer data type": 12345})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.find()
{ "_id" : ObjectId("5eccc2c2b8d2c3f9f691ab01"), "integer data type" : 12345 }
>


MaxKey/MinKey

MinKey type is used to compare a value against the lowest BSON elements.
MaxKey type is used to compare a value against the highest BSON elements.

Null

Null is used for storing the null values.

> db.BSON_Collection.insert({"Null data type": null})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.find().pretty()
{ "_id" : ObjectId("5eccc66db8d2c3f9f691ab0b"), "Null data type" : null }
>


Object

Object is used for storing the embedded documents.

> db.BSON_Collection.insert({"object data type": {"course name":"MongoDB Basics", "Duration in Months":2, "starting date":new Date("2020-01-30T16:00:00Z"), "Cost ($)":100.50}})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.find().pretty()
{
        "_id" : ObjectId("5eccc5feb8d2c3f9f691ab0a"),
        "object data type" : {
                "course name" : "MongoDB Basics",
                "Duration in Months" : 2,
                "starting date" : ISODate("2020-01-30T16:00:00Z"),
                "Cost ($)" : 100.5
        }
}
>


ObjectId

ObjectId() is used to generate a 12-digit objectid value. It consists of
  • 4-byte timestamp value
  • 5-byte random value
  • 3-byte incrementing counter

> x = ObjectId()
ObjectId("5ecccdbab8d2c3f9f691ab1f")
> x.getTimestamp()
ISODate("2020-05-26T08:05:14Z")
> x.toString()
ObjectId("5ecccdbab8d2c3f9f691ab1f")
> x.valueOf()
5ecccdbab8d2c3f9f691ab1f
>
This datatype is used to store the document's unique primary ID

Regular Expression

It is used to store regular expression document.

String


> db.BSON_Collection.insert({"string data type": "Welcome to MongoDB World"})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.find()
{ "_id" : ObjectId("5eccc3d5b8d2c3f9f691ab05"), "string data type" : "Welcome to MongoDB World" }
>


Symbol

It is similar to the string data type.

Timestamp


Timestamp data type is used to store a timestamp. It can be used When a document has been updated.
> db.BSON_Collection.insert({"Timestamp data type": new Timestamp()})
WriteResult({ "nInserted" : 1 })
> db.BSON_Collection.find().pretty()
{
        "_id" : ObjectId("5eccd537b8d2c3f9f691ab25"),
        "Timestamp data type" : Timestamp(1590482231, 1)
}
>


Additional Information: MongoDB JSON

  • All key must be surrounded by ".."
  • Keys and value must be separated by a colon
  • Each field must be separated by comma
  • data types are directly supported by JSON
  • Array - String, Embeded document
  • Array and Objects can be nested in any combination

{
        "_id" : ObjectId("5eccd537b8d2c3f9f691ab25"),
        "sections" : [
                "AG1",
                "MD1",
                "OA1"
        ],
        "grades" : [
                {
                        "key" : "A",
                        "value" : 100
                },
                {
                        "key" : "B",
                        "value" : 80
                },
                {
                        "key" : "C",
                        "value" : 60
                }
        ],
        "department" : {
                "name" : "Computer Lab",
                "incharge" : {
                        "first name" : "John",
                        "last name" : "Doe",
                        "dob" : ISODate("2000-05-23T00:00:00Z")
                }
        }
}