DataTypes In MongoDB

In this article, we will learn about the datatypes and their usage in MongoDB. If you are new to MongoDB, then I would recommend you to go through the previous articles of the series.
As we already know, MongoDB stores data in BSON format. BSON stands for the Binary-encoded format of JSON. Using BSON, we can make remote procedure calls in MongoDB. In MongoDB, each data type has an alias as well as a number which can be used for finding or searching the record in MongoDB. We will also learn about how to use that number/alias in searching in this article. We will use find () method which is used to select the document in the collection and is similar to the “Select” keyword used in SQL and another method is pretty () which makes easier to read the output of the document. Datatypes in MongoDB are mentioned below,
 
 
 
Let’s try some of the popular Datatypes of MongoDB.
 
Double
 
Double datatype is used to store floating values. In the below example, we have inserted the double data type (floating value) in the collection.

 
 
The code used in the above example -
  1. db.TestCollection.insert({"double data type":123.45})  
  2. db.TestCollection.find()    
String
 
MongoDB stores BSON Strings in UTF-8 format. Drivers for each programming language available convert the language’s string to UTF-8 when serializing and deserializing. 
 
 
 
The code used in the above example -
  1. db.TestCollection.insert({"string data type":"This is an example of String DataType"})  
  2. db.TestCollection.find()  
Object
 
Object Datatype is used for embedding the documents. Embedded Documents are the documents embedded in another document in the form of a Key - Value pair.  
 
 
 
The code used in the above example -
  1. var embeddedObject={"English":90,"Maths":85,"Physics":75,"Chemistry":80,"Computer Science" :90}  
  2. db.TestCollection.insert({"Object data type":"This is an example of Object DataType","Marks":embeddedObject})  
Array
 
Array datatype is used to store the array. With an array data type, we can store multiple values in a single key of the document. 
 
 
 
The code used in the above example -
  1. var arrayQualification=["BCA","MCA"];  
  2. db.TestCollection.insert({"Object data type":"This is an example of Array DataType","Qualification":arrayOualification}) db.TestCollection.find().pretty()  
ObjectId
 
ObjectId is unique, small, and fast to generate. Its values are of 12 Bytes out of which the first four Bytes for the timestamp (that reflects ObjectID creation in the seconds since the UNIX epoch), 5 bytes for a random value and 3 bytes counter, starting with a random value. In MongoDB, if a document is inserted without the _id field, Mongo will generate a unique _id that acts as a primary key for the document in the collection.
 
 
 
Boolean
 
Boolean Datatype stores the Boolean values, i.e., true/false. 
 
 
 
The code used in the above example -
  1. db.TestCollection.insert({"Object data type":"This is an example of Boolean DataType","Nationality Indian":true})  
  2. db.TestCollection.find() 
Null
 
Null Datatype is used to store null values (also for non-existent fields) in it.
 
 
The code used in the above example -
  1. db.TestCollection.insert({"Object data type":"This is an example of Null DataType","EmailID":null})  
  2. db.TestCollection.find()   
Date
 
Date Datatype is used to store in date or time in the Unix time format.
 
 
The code used in the above example -
  1. var date=new Date()  
  2. var date2=new ISODate()  
  3. var date3=Date()  
  4. var month=date2.getMonth()  
  5. db.TestCollection.insert({"Date":date,"Date2":date2,"Date3":date3,"Month":month})  
  6. db.TestCollection.find().pretty()  
Date() returns Date as a string. new Date() and ISODate() returns Date object wrapped in ISODate() wrapper. With the help of getMonth() method, we can get the month for the Date object. Months are zero indexed so in the case of October, we are getting 10 as the value.
 
Timestamp
 
Timestamp values are 64-bit value out of which first 32 bits the are time_t value (seconds since the UNIX epoch) and the second 32 bits are incremental ordinals for operations within a given second. For single instance of the mongoD, timestamp values are always unique.
 
 
 
The code used in the above example -
  1. var ts=new Timestamp()  
  2. ts  
  3. Timestamp(0, 0)  
  4. db.TestCollection.insert({"Object data type":"This is an example of Timestamp","Timestamp":ts})  
  5. db.TestCollection.find.pretty()   
Integer
 
In MongoDB, Integer can be 32 and 64-bit depending upon the server.
 
 
The code used in the above example -
  1. db.TestCollection.insert({"Integer Example":10})  
  2. db.TestCollection.find().pretty()  
As we said, Alias or Number mentioned in the Datatype table are useful in the filtering the documents of the collection. Let’s try to search records with alias/number provided by the MongoDB.
 
 
The code used in the above example -
  1. db.Employee.insert({"Name":"Anoop Sharma",Codc:"ABC123"})  
  2. db.Employee.insert(("Name":"Andrew",Code:321))  
  3. db.Employee.insert(("Name":"Rahul",Code:"456")  
  4. db.Employee.find()  
  5. db.Employee.find({"Code":{$type:"string"}})  
  6. db.Employee.find({"Code":{Stype:2}})  
  7.    
In the above example, we have added Name and Code in Employee collection. In the first record and third record, we have added code with string datatype and in the second record, the code is for an integer type. Firstly, we check the data in the Employee collection with the find() method. $type selects the documents where the value of the field is an instance of the specified BSON type(s). Querying by data type is useful when dealing with highly unstructured data where data types are not predictable. The same thing happens with the Code key in the Employee collection. Pass the value as number or alias in order to filter the documents in the collection. In above example, we filtered the data which has the string datatype code.
 
Hope this will help you. Thanks. 


Similar Articles