Discuss Datatypes in MongoDB?

In MongoDb , the documents are stored in BSON, which is the binary encoded format of JSON and using BSON we can make remote procedure calls in MongoDB. BSON data format supports various datatypes.

String is the most commonly used data type in MongoDB to store data, BSON strings are of UTF-8. So, the drivers for each programming language convert from the string format of the language to UTF-8 while serializing and de-serializing BSON. The string must be a valid UTF-8

switched to


>db.student.insertMany([{name:”Akshay”},{name:”Vikash”}])

{

       “acknowledged” : true,

       “insertedIds”    : [

              ObjectId(“601af2dd6fd54aa34c9c6df3”),

              ObjectId(“601af2dd6fd54aa34c9c6df4”)                                 

                ]

}

>db.student.find().pretty()

{  “ _id” : ObjectId(“601af2dd6fd54aa34c9c6df3”), “name” : “Akshay”}

{  “_id”  : ObjectId(“601af2dd6fd54aa34c9c6df4”), “name” : “Vikash”}

>

In MongoDB, the Integer data type is used to store an integer value. We can store integer data type in two forms 32 -bit signed integer and 64 – bit signed integer.

>db.student.insertOne({name : “Akash”, age : 19})

{

       “acknowledged” : true,

       “insertedId” :  
         ObjectId(“601af2dd6fd54aa34c9c6df5”)

}

>db.student.find().pretty()

{  “ _id” : ObjectId(“601af2dd6fd54aa34c9c6df3”), “name” : “Akshay”}

{  “_id”  : ObjectId(“601af2dd6fd54aa34c9c6df4”), “name” : “Vikash”}

{

        “_id” : ObjectId(“601af2dd6fd54aa34c9c6df5”),

        “name”: “Akash”,

         “age” : 19

}

>

The Double data type is used to store the floating-point values.

>db.student.insertOne({name : “Sagar”, age : 20, marks 546.43})

{

          “acknowledged” : true,

          “insertedId” : ObjectId(“601af2dd6fd54aa34c9c6df6”)

}

>db.student.find().pretty()

{  “ _id” : ObjectId(“601af2dd6fd54aa34c9c6df3”), “name” : “Akshay”}

{  “_id”  : ObjectId(“601af2dd6fd54aa34c9c6df4”), “name” : “Vikash”}

{

                “_id” : ObjectId(“601af2dd6fd54aa34c9c6df5”),

          “name”: “Akash”,

           “age” : 19

}

{

          “_id” : ObjectId(“601af2dd6fd54aa34c9c6df6”),

           “name”: “Sagar”,

            “age” : 20

            “marks” : 546.43

}

>

The Boolean data type is used to store either true or false

>db.student.insertOne({name : “vishal”, pass:true})

{

         “acknowledged” : true,

         “insertedId” : ObjectId(“601af2dd6fd54aa34c9c6df7”)

}

>db.student.find().pretty()

{ “ _id” : ObjectId(“601af2dd6fd54aa34c9c6df3”), “name” : “Akshay”}

{ “_id” : ObjectId(“601af2dd6fd54aa34c9c6df4”), “name” : “Vikash”}

{ 

         “_id” : ObjectId(“601af2dd6fd54aa34c9c6df5”),

          “name”: “Akash”,

          “age” : 19

}

{ 

          “_id” : ObjectId(“601af2dd6fd54aa34c9c6df6”),

           “name”: “Sagar”,

           “age” : 20

           “marks” : 546.43

}

{ 

           “_id” : ObjectId(“601af2dd6fd54aa34c9c6df7”),

            “name”: “vishal”,

             “pass” : true

}

>

The Null data type is used to store the null value.

The Array is the set of values. It can store the same or different data types values in it. In MongoDB, the array is created using square brackets([]).

>db.student.find().pretty()

{  “ _id” : ObjectId(“601af2dd6fd54aa34c9c6df3”), “name” : “Akshay”}

{  “_id”  : ObjectId(“601af2dd6fd54aa34c9c6df4”), “name” : “Vikash”}

{

           “_id” : ObjectId(“601af2dd6fd54aa34c9c6df5”),

           “name”: “Akash”,

           “age” : 19

}

{

          “_id” : ObjectId(“601af2dd6fd54aa34c9c6df6”),

          “name”: “Sagar”,

          “age” : 20

          “marks” : 546.43

}

{

            “_id” : ObjectId(“601af2dd6fd54aa34c9c6df7”),

            “name”: “vishal”,

            “pass” : true

}

{

            “_id” : ObjectId(“601af2dd6fd54aa34c9c6df7”),

             “name”: “Akash”,

             “MobNo” : null,

             “skills” : [

                           “c”,

                           “c++”,

                            “java”,

                            “python”,

                            “JS”

                ]

}

>

Object data type stores embedded documents. Embedded documents are also known as nested documents. Embedded document or nested documents are those types of documents which contain a document inside another document.