Mongo DB Data Type explanations, MongoDB supports more than 15 data types but more important is that what kind of data type is basically used to store the documents. MongoDB uses bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments called BSON to store documents and make remote procedure calls.
Hello world! We have seen How to do CRUD operation in MongoDB earlier now it’s time to know what are data type which helps us create more efficient database using MongoDB. MongoDB supports more than 15 data types but more important is that what kind of data type is basically used to store the documents.

MongoDB uses bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments called BSON to store documents and make remote procedure calls. Like JSON, BSON sup­ports the em­bed­ding of doc­u­ments and ar­rays with­in oth­er doc­u­ments and ar­rays. BSON also con­tains ex­ten­sions that al­low rep­res­ent­a­tion of data types that are not part of the JSON specifications. Being more specific BSON is a binary format in which zero or more ordered key/value pairs are stored as a single entity. We call this entity a document.

Data type is an important part of Database. When you store the data in database with proper data type it reflects faster performance, therefore data type is important. For e.g. a number can be stored as an integer or string both but if we require numbers later for mathematical calculation then it is good to store the numerical value as integer to get faster mathematical calculations.

In MongoDB each BSON type has both integer and string identifiers as listed in the following table:

Type Number Alias Notes
Double 1 “double”
String 2 “string”
Object 3 “object”
Array 4 “array”
Binary data 5 “binData”
Undefined 6 “undefined” Deprecated.
ObjectId 7 “objectId”
Boolean 8 “bool”
Date 9 “date”
Null 10 “null”
Regular Expression 11 “regex”
DBPointer 12 “dbPointer” Deprecated.
JavaScript 13 “javascript”
Symbol 14 “symbol” Deprecated.
JavaScript (with scope) 15 “javascriptWithScope”
32-bit integer 16 “int”
Timestamp 17 “timestamp”
64-bit integer 18 “long”
Decimal128 19 “decimal” New in version 3.4.
Min key -1 “minKey”
Max key 127 “maxKey”

Some of the most important data types are explained below:

String

String data type is the most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid. MongoDB $regex queries support UTF-8 in the regex string.

NumberDecimal

The decimal BSON data type uses the IEEE 754 decimal128 floating-point numbering format which supports 34 decimal digits (i.e. significant digits) and an exponent range of −6143 to +6144. Decimal 128 datatype is introduced in 3.4 version of MongoDB.

NumberLong (Integer)

The mongo shell treats all numbers as floating-point values by default. The mongo shell provides the NumberLong() wrapper to handle 64-bit integers.

NumberInt (Integer)

The mongo shell treats all numbers as floating-point values by default. The mongo shell provides the NumberInt() constructor to explicitly specify 32-bit integers.

Date

BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). This results in a representable date range of about 290 million years into the past and future.


Timestamp

BSON has a special timestamp type for internal MongoDB use and is not associated with the regular Date type. Timestamp values are a 64 bit value where the first 32 bits are a time_t value (seconds since the Unix epoch) and the second 32 bits are an incrementing ordinal for operations within a given second.

Within single mongod instance timestamp values are always unique.

Array

Arrays are the data type that can store single or multiple arrays or values within a single key.

Object

Objects are the data type that used for embedded documents

Object ID

ObjectIds are small unique ID that generated very fast and ordered. ObjectId values consist of 12 bytes, where the first four bytes are a timestamp that reflect the seconds since the Unix epoch, 5-byte random value, and last 3-byte counter, starting with a random value.

In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.

MongoDB Tutorial : How to get lowercase hexadecimal string from MongoDB ObjectId() – Mongodb Objectid Operations.

Boolean

This data type is store Boolean values i.e. True or False.

Min/Max Keys

This type is used to compare a value against the lowest and highest BSON elements. The min and max keys indicate that the system should avoid normal query planning. Instead they construct an index scan where the index bounds are explicitly specified by the values given in min and max.

Null

This data type is used to store null data.

Binary data

This data type is used to store binary form of data.

Regular Expression

This data type is used to store regular expression.

Data Modeling in MongoDB

Unlike SQL databases, where you must determine and declare a table’s schema before inserting data, MongoDB’s collections, by default, does not require its documents to have the same schema. Data in MongoDB has a flexible schema documents in the same collection. They offer to create different fields, structure and data types in and common fields in same collection. This flexibility gives you data-modeling choices to match your application and its performance requirements.


progrramers-logo

progrramers

Hello friends! Progrramers is a tutorial site of w3 programming. If you like this tutorial site please encourages us by sharing this site links with your friends and nears & dears who want to learn web development and give us like on our Facebook page. If have any question please type in to comment box or send us message on social media sites via below given social links. Thank you, have a nice learning.

Post A Comment:

4 comments:

  1. Awesome blog. I enjoyed reading your articles. This is truly a great read for me.


    data analytics course

    data science course

    business analytics course

    ReplyDelete
  2. I am impressed by the information that you have on this blog. It shows how well you understand this subject.
    data analytics course
    data science course
    big data course
    big data course
    360DigiTMG

    ReplyDelete
  3. Wow what a Great Information about World Day its exceptionally pleasant educational post. a debt of gratitude is in order for the post.
    data science course in malaysia
    data science certification
    data science course malaysia
    data science malaysia
    data scientist course malaysia

    ReplyDelete
  4. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.

    PMP Certification

    ReplyDelete