Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Sunday, 18 November 2018

DataTypes in MongoDB

In this article, we will learn about the Datatypes and its usage in the MongoDB.  If you are new to MongoDB then I would recommend you to go through the previous article 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. Datatype in MongoDB are mentioned below:
Let’s try some of the popular Datatypes of the MongoDB:
1. Double: Double Data type is used to store floating values. In the below example, we have inserted the double data type (floating value) in the collection
2. String: MongoDB stores BSON Strings in UTF-8 format. Drivers for each programming language converts the language’s string to UTF-8 when serializing and deserializing.
3. Object: Object Datatype is used for embedding the documents. Embedded Document are the documents embedded in another document in the form of a Key - Value pair.
4. 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.
5. ObjectId: ObjectId is likely unique, small, fast to generate. Its values are of 12 Bytes out of which first four Byte stands for 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.
6. Boolean: Boolean Datatype stores the Boolean values, i.e. true/false.
7. Null: Null Datatype is used to store null values (also for non-existent fields) in it.
8. Date: Date Datatype is used to store in date or time in the Unix time format.
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.
9. 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 ordinal for operations within a given second. For single, Instance of the mongoD, timestamp values are always unique.
10. Integer: In MongoDB, Integer can be 32 and 64-bit depending upon the server.
As we said Alias or Number mentioned in the Datatype table useful in the filtering the documents of the collection. Let’s try to search records with alias/number provided by the MongoDB.

In above example, we have added Name and Code in Employee collection. In 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 have Code as string datatype.

Hope this will help you. Thanks.


1 comment:

  1. Looks like a typo under the description for the "Date" type. Month is "November", so returns a 10.

    ReplyDelete

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook