Tuesday, May 12, 2020

MongoDb Data Types

Data type is an essential component of a language or script that is used to define the type of data being used in framing the database. It is important for you to know that MongoDB stores data in BSON format. In this chapter, you will learn about the different data types that exist in MongoDB, along with their implementation techniques.

What Are JSON and BSON?

JavaScript Object Notation (JSON) is a standard file format that uses human type readable text to transmit data with attribute-value pairs and array data types. This is one of the most common data formats which are mainly used for asynchronous browser-server communication. JSON is a language-independent format. BSON, on the other hand, is a computer interchange format that is mainly used for data storage and as a network transfer format in the MongoDB database. It is a simple binary form which is used to represent data structures and associative arrays (often called documents or objects in MongoDB).


Key Differences between JSON and BSON

Both are popular choices in the market; let us discuss some of the major difference:

BSON is a serialization format encoding format for JSON mainly used for storing and accessing the documents whereas JSON is a human-readable standard file format mainly used for transmission of data in the form of key-value attribute pairs.

BSON is designed such that it consumes less space, but it is not extremely efficient than JSON. BSON in fact in some cases uses more space than JSON. The reason for this is traversability which means that BSON adds some additional information to documents like string length and sub-objects which in turn makes the traversing faster.

Different MongoDB Data Types

Remote procedure calls in MongoDB can be made by using the BSON format. MongoDB has a unique way of representing data types in which each data type is associated with an alias as well as a number that is usually used to search or find any specific record within a MongoDB database. MongoDB allows its users to implement different variations of data types:



Integer is a data type that is used for storing a numerical value, i.e., integers as you can save in other programming languages. 32 bit or 64-bit integers are supported, which depends on the server.

db.TestCollection.insert({"Integer example": 62})



String is one of the most frequently implemented data type for storing the data.

db.TestCollection.insert({"string data type" : "This is a sample message."})



Double, as we know, is used to store float values. It represents the float value and is of 8 bytes. Double is preferred to store and retrieve decimal values as known in SQL. 

db.TestCollection.insert({"double data type": 3.1415})



Null is implemented for storing a Null value.

db.TestCollection.insert({" EmailID ": null})



Date is implemented for storing the current date and time as UNIX-time format.

var date=new Date()

var date2=ISODate()

var month=date2.getMonth()




Boolean is implemented for storing a Boolean (i.e., true or false) values.

db.TestCollection.insert({"Nationality Indian": true})



Arrays are implemented for storing arrays or list type or several values under a single key.

var degrees = ["BCA""BS""MCA"]

db.TestCollection.insert({" Array Example" : " Here is an example of array"," Qualification" : degrees})



Object is implemented for embedded documents.

var embeddedObject = {"English" : 94"ComputerSc." : 96"Maths" : 80,

"GeneralSc." : 85}

db.TestCollection.insert({"Object data type" : "This is Object",

"Marks" : embeddedObject})


Min/Max Keys

Min / Max keys are implemented for comparing a value adjacent to the lowest as well as highest BSON elements.


Regular expression

Regular expression is implemented for storing regular expression.



Code is implemented for storing JavaScript code for your MongoDB document. 

Binary data

Binary data is implemented for storing binary data.

Object ID

Object ID is implemented for storing the ID of the document.