EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Data Types
 

MongoDB Data Types

Sulaksh More
Article bySulaksh More
EDUCBA
Reviewed byRavi Rathore

Updated February 28, 2023

MongoDB Data Types

 

 

Introduction to MongoDB Data Types

For any programming or scripting language or a database, Data types are important, to identify what type of data it is. If you’ve worked with SQL or MySQL, you are certainly aware of the table structured output and storage, but with MongoDB Data Types the case is altogether different. Nothing is stored in table format here, and neither is retrieved in such a way. Now, when we say that mongo stores data in BSON format, it does not mean that JSON is out of the picture here, BSON is actually a binary format of JSON, an extended version of JSON.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Below is the screenshot of MongoDB version command:

MongoDB Data Types1

Explanation: In the above screenshot, version details and the environment and Git & OpenSSL version details are reflected.

MongoDB Data Types

MongoDB offers various data types. Each data type has a specific application and syntax.

String: One of the most widely used data type, String is UTF 8 (Unicode Transformation Format). Whatever goes between “ ” is a string. There are various string operations that MongoDB supports.

Numeric Data Type

For storing numeric values, MongoDB offers two variants with Integer and a Float Data Type.

Integer

Simple plain numbers without decimal points, 32 bytes or 64 bytes, are saved as Int and retrieves as same.

Code:

db.samplecoll.insert({"id":"01","name":"mongodb"})

Above query will insert a new record in samplecoll collection. In the below-attached screenshot, we have demonstrated the use of String and Integer.

Output:

db.samplecoll.find()
db.samplecoll.find().pretty()

MongoDB Data Types2

Explanation: “id” is our integer data type with a value of 01 and “name” is our String data type with the name MongoDB. If you notice, we have executed find() query twice. First, if plain finds query, which displays the data as it is, while with second find(). Pretty () we have had the data reflected in a JSON format, key-value pairs.

Double

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.

Code:

db.samplecoll.insert({"id":"03","name":"doublevalue", "Double":"87.2637"})

This query will insert a record with Double as a key and 87.2637 as a value. Refer the below screenshot and check the key-value pair. The last key is Double, and value is 87.2637, which is a double data type.

Output:

db.samplecoll.find().pretty()

MongoDB Data Types3

Boolean

Like any other programming language or DB, MongoDB recognizes Boolean to simply store, either True or False. Logical

Null

Again, just like the name says, Null. Null values and non-existent values are stored in the Null data type.

Code:

db.samplecoll.insert({"id":"02","name":"AnotherDB", "IsActive":true, "null":})

This query will insert another record, with a boolean value of true and a null key-value pair, with nothing or null value. Refer below screenshot for implementation of Boolean and Null values.

Output:

<b.samplecoll.find().pretty()

MongoDB Data Types4

Explanation: As you can see, we have inserted another record here, with id as 02 and name as AnotherDB. We have added another boolean key value with IsActive as true. And for the null, we have a key as null, and value is null as you can see.

Timestamp

Just as the name suggests, it has to do everything with the timestamp, meaning recording the event’s time. Every time stamp is of 64-bit value and has two parts of 32 bit each.

  • time_t is the first part which is of 32 bit.
  • Remaining 32 bit is incremental ordinal is for operations.

These timestamp values are always unique based on the time of document inserted or modified.

Code:

db.samplecoll.update( { _id: 11 }, { $set: { Name: "NewDB" }, $setOnInsert: { Date: new Date() } }, { upsert: true } );

With the above query’s execution, a new record will be inserted with _id as 11, Name as NewDB and a Date with current system date. Refer below screenshot.

Output:

db.samplecoll.find().pretty()

NewDB

ObjectId

Like we have ID in SQL, ObjectId is for MongoDB, uniquely generated with every new record inserted. Even when we inserted simple String in mongo, “_id” was generated with a distinctive ObjectId.

Min/ Max keys: These are used to compare and return the lowest and highest BSON value in a document. Majorly used for internal operations.

Arrays: Array is simply used to store and retrieve list or collection of values or multiple values for a single key.

Code:

db.samplecoll.update( {"Name":"NewDB"}, {"$pushAll" : {Cities : ["Mumbai","Pune","Delhi"]}} )

Now, we have used an update query here, just for a change. We have updated that we’ve added another key-value pair, which is of an array data type. “Cities” is the key while it has multiple values. When you execute the find query, arrays will be reflected vertically, inside square brackets. Refer below screenshot for the proper output of an above query.

Output:

db.samplecoll.find().pretty()

ObjectId

Symbol

Highly similar to the String Data Type, but not supported by the mongo shell.

If the symbol is retrieved upon query execution, it will be converted into a string then displayed on the shell.

Code:

var symbol="uh%^&@#"
db.samplecoll.insert({_id:ObjectId(),name:"sample",Symbol:symbol})
db.samplecoll.find({name:”sample”}).pretty()

Output:

Symbol

Binary Data Type

Simply, to store and be able to retrieve the binary data format. Binary Data Format is used to store raw byte data, like IP addresses.

Regular expression: Used to store regular expressions, directly mapping to JavaScript. Also, an improvised way to recognize patterns in a complex, large amount of data. Just a simple and plain way to store and retrieve JavaScript code in any collection in MongoDB. Compared to SQL, MongoDB has Collections instead of Tables and Documents instead of rows, and Mongo does not follow Row and Columns format.

Conclusion

MongoDB is a NoSQL, Document Oriented Database. Data types define how we store information in the database. Every data type has a specific use. We’ve learned about various data types that MongoDB has to offer. Along with queries on particular data type, we’ve understood the storing and retrieving information according to data types, with respective screenshots. These data types can be used as per requirement.

Recommended Articles

This is a guide to MongoDB Data Types. Here we discuss introducing MongoDB Data Types, Numeric data types, and Binary data type with examples. You can also go through our other related articles to learn more –

  1. Advantages of MongoDB
  2. MongoDB Tools
  3. MongoDB MapReduce | Examples
  4. Complete Guide to MongoDB Geospatial
  5. How to Works MongoDB count?

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW