MongoDB (NoSQL DB) supported data type
3 min read
In my last article about MongoDB, I wrote about what it is and why should you learn it. Today we'll learn a bit more about the data types that we can store in MongoDB.
JSON vs BSON
For the most part, JSON and BSON are very similar. While using Mongo shell or accessing it through a node driver we can hardly see the difference between JSON and BSON.
BSON has additional data types and is much more efficient for storing and retrieving data. BSON achieves efficiency due to the fact that it stores type and length data when encoding information.
For a more detailed comparison, you can read the official MongoDB article here.
Now let's have a look at a list of some important MongoDB data types.
ObjectId: This is a unique identifier that is generated for each document in a MongoDB collection. It is a 12-byte value that consists of a 4-byte timestamp, a 3-byte machine ID, a 2-byte process ID, and a 3-byte counter. Objectives are often used as the default value for the "_id" field in a MongoDB document.
32-bit integer (int): This data type represents a signed 32-bit integer value. It can store values between -2147483648 and 2147483647.
64-bit integer (float): This data type represents a signed 64-bit floating-point value. It can store values with higher precision and range than the 32-bit integer data type.
Double: This data type represents a double-precision floating-point value. It can store values with higher precision and range than the 64-bit integer data type.
Decimal128: This data type represents a 128-bit decimal value. It allows you to store numbers with a high degree of precision, making it useful for financial applications.
String: This data type represents a sequence of Unicode characters. Strings can be of any length and can include any valid Unicode character.
Object: This data type represents an embedded document in MongoDB. It allows you to store nested data structures within a document.
Array: This data type represents a list of values. Arrays can contain any valid data type, including other arrays.
Binary data: This data type represents a sequence of binary data. It can be used to store images, audio, or other types of binary data.
Boolean: This data type represents a true or false value.
Date: This data type represents a date and time value. It stores the number of milliseconds since the Unix epoch (January 1, 1970).
Timestamp: This data type represents a timestamp value, which consists of a timestamp and a counter. It is used to store the creation and modification times for a document.
Note that there are some addition data types offered by MongoDB which you can read in more detail here.
Do let me know if you've any feedback or think something's missing...
Did you find this article valuable?
Support Jayesh Karli by becoming a sponsor. Any amount is appreciated!