Open In App

DataTypes in MongoDB

Last Updated : 09 Jun, 2022
Like Article

In MongoDB, the documents are stores in BSON, which is the binary encoded format of JSON and using BSON we can make remote procedure calls in MongoDB. BSON data format supports various data-types. Below are the enlisted MongoDB data types:

1. String: This is the most commonly used data type in MongoDB to store data, BSON strings are of UTF-8. So, the drivers for each programming language convert from the string format of the language to UTF-8 while serializing and de-serializing BSON. The string must be a valid UTF-8. 

Example: In the following example we are storing the name of the student in the student collection:

Here, the data type of the value of the name field is a string.

2. Integer: In MongoDB, the integer data type is used to store an integer value. We can store integer data type in two forms 32 -bit signed integer and 64 – bit signed integer.

Example: In the following example we are storing the age of the student in the student collection:

3. Double: The double data type is used to store the floating-point values. 

Example: In the following example we are storing the marks of the student in the student collection:

4. Boolean: The boolean data type is used to store either true or false.

Example: In the following example we are storing the final result of the student as pass or fail in boolean values.

5. Null: The null data type is used to store the null value.

Example: In the following example,  the student does not have a mobile number so the number field contains the value null.

6. Array: The Array is the set of values. It can store the same or different data types values in it. In MongoDB, the array is created using square brackets([]). 

Example: In the following example, we are storing the technical skills of the student as an array.

7. Object: Object data type stores embedded documents. Embedded documents are also known as nested documents. Embedded document or nested documents are those types of documents which contain a document inside another document.

Example: In the following example, we are storing all the information about a book in an embedded document.

8. Object Id: Whenever we create a new document in the collection MongoDB automatically creates a unique object id for that document(if the document does not have it). There is an _id field in MongoDB for each document. The data which is stored in Id is of hexadecimal format and the length of the id is 12 bytes which consist:

  • 4-bytes for Timestamp value.
  • 5-bytes for Random values. i.e., 3-bytes for machine Id and 2-bytes for process Id.
  • 3- bytes for Counter

You can also create your own id field, but make sure that the value of that id field must be unique.

Example: In the following example, when we insert a new document it creates a new unique object id for it.

9. Undefined: This data type stores the undefined values.

Example: In the following example the type of the duration of the project is undefined.

10. Binary Data: This datatype is used to store binary data. 

Example: In the following example the value stored in the binaryValue field is of binary type.

11. Date: Date data type stores date. It is a 64-bit integer which represents the number of milliseconds. BSON data type generally supports UTC datetime and it is signed. If the value of the date data type is negative then it represents the dates before 1970. There are various methods to return date, it can be returned either as a string or as a date object. Some method for the date:

  • Date(): It returns the current date in string format.
  • new Date(): Returns a date object. Uses the ISODate() wrapper. 
  • new ISODate(): It also returns a date object. Uses the ISODate() wrapper.

Example: In the following example we are using all the above method of the date:

12. Min & Max key: Min key compares the value of the lowest BSON element and Max key compares the value against the highest BSON element. Both are internal data types.


13. Symbol: This data type similar to the string data type. It is generally not supported by a mongo shell, but if the shell gets a symbol from the database, then it converts this type into a string type.


14. Regular Expression: This datatype is used to store regular expressions.

Example: In the following example we are storing the regular expression gfg:

15. JavaScript: This datatype is used to store JavaScript code into the document without the scope.

Example: In this example, we are using the JavaScript syntax in the shell:

16. JavaScript with Scope: This MongoDB data type store JavaScript data with a scope. This data type is deprecated in MongoDB 4.4.

Example: In this example, we are using the JavaScript syntax in the shell:

17. Timestamp: In MongoDB, this data type is used to store a timestamp. It is useful when we modify our data to keep a record and the value of this data type is 64-bit. The value of the timestamp data type is always unique.


18. Decimal: This MongoDB data type store 128-bit decimal-based floating-point value. This data type was introduced in MongoDB version 3.4



Previous Article
Next Article

Similar Reads

Connect MongoDB (AWS) from Local Machine using WinSCP and MongoDB Compass
Pre-requisite: AWS and MongoDB In this article, we are going to connect to the Mongo database of the AWS Ubuntu Server instance using WinSCP and learn how to get connected to the server from your local machine using MongoDB Compass.  If you haven't installed a MongoDB server in your AWS EC2 instance then follow the steps present in this article and
4 min read
MongoDB - Delete Multiple Documents Using MongoDB Shell
The db.collection.deleteMany() method is used to delete multiple documents from a collection in Mongo Shell. This method deletes multiple documents from the collection according to the filter. The deleteMany() is a Mongo shell method, which can delete multiple documents. This method can be used in multi-document transactions. If you use this method
2 min read
Connect MongoDB Atlas Cluster to MongoDB Compass
MongoDB Compass is a free GUI for MongoDB. You might want to connect MongoDB Atlas Cluster to MongoDB Compass to take benefit of the GUI model for database administration. This guide explains how to connect MongoDB Atlas Cluster to MongoDB Compass. The connection process involves obtaining a connection string from MongoDB Atlas, configuring MongoDB
4 min read
MongoDB Compass vs MongoDB Atlas
MongoDB is a popular NoSQL database used by developers worldwide for its flexibility, scalability and ease of use. Two key components of the MongoDB ecosystem are MongoDB Compass and MongoDB Atlas. While both are essential tools for working with MongoDB databases, they serve different purposes and cater to different user needs. In this article, We
4 min read
MongoDB: An introduction
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term 'NoSQL' means 'non-relational'. It means that MongoDB isn't based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This format of storage is called BSON ( similar to JSON f
5 min read
Nodejs - Connect MongoDB with Node app using MongooseJS
Connect the MongoDB database using MongooseJS to our NodeJS application Before we dive into looking how the mongoDB connects with a nodejs app using mongooseJS, lets get a brief intro to these technologies which are ruling the web development domain of today. Node: Node.js (Node) is an open source development platform for executing JavaScript code
3 min read
MongoDB vs MySQL
Differences between MongoDB and MySQL on various parameters are discussed below: What are MongoDB and MySQL? MongoDB MySQL MongoDB is an open-source database developed by MongoDB, Inc. MongoDB stores data in JSON-like documents that can vary in structure. It is a popular NoSQL database. MySQL is a popular open-source relational database management
2 min read
Difference between Oracle and MongoDB
1. Oracle : Oracle is a relational database management system (RDBMS). It was developed by Oracle Corporation in 1980. It is the first database designed for grid computing that provides the most flexible and cost-effective way to manage information and application. It runs on major platforms like Windows, Unix, Linux, and macOS. It is a relational
2 min read
Connecting MongoDB to Jupyter Notebook
MongoDB is one of the most famous NoSql databases. It is an open-source database. The simple meaning of the NoSql database is a non-relational database. i.e., It doesn't include the specific structure of databases such as tables and all like the relational database. So, you can design the schemas without any restrictions. Jupyter Notebook is also a
3 min read
Introduction about Node.js and MongoDB
NoSQL databases are databases that store and retrieve the data that is present in a non-tabular format. Depending on the format of the data stored, NoSQL databases are split into 4 major types: Key-Value Graph database Document-oriented Column family These databases are built to address the shortcomings of traditional RDBMS and provide high perform
4 min read
Article Tags :