In Module 3, we discussed the difference between JSON and BSON. We learned that MongoDB stores data as BSON (Binary JSON) to increase speed and expand the available data types.
In this module, we will explore the most commonly used BSON data types you will encounter when building applications. Understanding these types is crucial for properly designing your database schema later in the course.
1. String
The String type is the most common data type in MongoDB. It is used to store text. BSON strings are strictly UTF-8 encoded, meaning you can safely store text in almost any language, as well as emojis.
{
"firstName": "Suresh",
"bio": "Loves programming and ☕"
}
2. Number (Integer and Double)
Unlike standard JSON, which treats all numbers the same, BSON distinguishes between different types of numbers to optimize storage space and calculation speed.
32-bit Integer (Int32)
Used for standard, whole numbers. If you type a whole number into the MongoDB shell, it will often default to a double or a 64-bit integer depending on your driver, but you can explicitly cast it if needed.
{ "age": 25 }
64-bit Integer (Long / Int64)
Used for incredibly large whole numbers (e.g., counting billions of page views or tracking unique mathematical identifiers).
{ "totalViews": NumberLong("9876543210") }
Double (Floating Point)
Used for numbers with decimal points. This is the default numeric type for the MongoDB Shell.
{ "temperature": 98.6 }
3. Boolean
The Boolean type is used to store simple true or false values. It is highly efficient and perfect for flags.
{
"isActive": true,
"hasPaidSubscription": false
}
4. Date
Standard JSON does not have a Date type (dates in JSON are usually just strings like "2026-07-05"). BSON, however, has a dedicated Date type.
The BSON Date type stores the date as a 64-bit integer representing the number of milliseconds since the Unix epoch (January 1, 1970). This allows you to perform date math directly in the database (e.g., finding all users who signed up in the last 7 days).
{
"createdAt": ISODate("2026-07-05T12:00:00Z")
}
Note: Always try to store dates using the BSON Date type rather than standard strings, or you will lose the ability to easily sort or filter chronologically!
5. Array
An Array is used to store lists of data. The data inside the array can be of any type, and you can even mix data types within the same array (though it is generally best practice to keep them uniform).
{
"favoriteColors": ["Red", "Blue", "Green"],
"testScores": [95, 88, 100],
"mixedBag": ["Text", 42, true]
}
6. Object (Embedded Document)
An Object (also called an Embedded Document) allows you to nest a document inside another document. This is what makes MongoDB so powerful compared to relational databases, as you can store related data together.
{
"username": "coder123",
"address": {
"street": "123 Main St",
"city": "Techville",
"zipCode": "90210"
}
}
You can even query embedded fields using “dot notation” (e.g., db.users.find({ "address.city": "Techville" })).
7. Null
The Null type is used to explicitly indicate that a field exists but has no value.
{
"middleName": null
}
Note: Having a field set to null is different from the field simply not existing ($exists: false). Use null when the field is expected but empty.
8. ObjectId
The ObjectId is perhaps the most unique BSON data type. It is a 12-byte value automatically generated by MongoDB to uniquely identify documents. It is always used for the _id primary key.
An ObjectId is incredibly clever. It is not just random gibberish; it actually contains:
- A 4-byte timestamp (representing when it was created).
- A 5-byte random value.
- A 3-byte incrementing counter.
Because it contains a timestamp, you can actually extract the creation date of a document directly from its _id, without needing a separate createdAt field!
{
"_id": ObjectId("507f1f77bcf86cd799439011")
}
9. Binary Data
The Binary Data type allows you to store arrays of bytes. This is typically used to store small files directly inside the database, such as small image thumbnails, PDF attachments, or encrypted payload data.
Warning: MongoDB documents have a strict size limit of 16MB. If you try to store a 50MB 4K video as Binary Data, the database will throw an error. For massive files, MongoDB offers a separate solution called GridFS.
10. Decimal128
Standard floating-point numbers (Doubles) are notoriously bad for financial calculations. Because of how computers handle floating-point math, calculating $0.10 + $0.20 can sometimes result in $0.30000000000000004. If you are building a banking app, that tiny fraction of a cent can ruin your accounting.
To solve this, BSON introduced the Decimal128 type. It provides 128-bit decimal-based floating-point precision, ensuring that financial calculations are perfectly accurate.
{
"accountBalance": NumberDecimal("1000.50"),
"itemPrice": NumberDecimal("9.99")
}
Rule of Thumb: If you are dealing with money, always use Decimal128 (or store the value as an integer representing cents).
Summary
BSON significantly enhances standard JSON by giving developers powerful, strongly-typed tools to model their data.
- ObjectId handles unique primary keys effortlessly.
- Date allows for complex time-based calculations.
- Object and Array allow you to build rich, deeply nested document structures.
- Decimal128 ensures your e-commerce and banking apps don’t lose money to rounding errors.
Now that we know the types of data we can store, and how to query it, it’s time to learn how to make those queries lightning fast in Module 8: Indexing!
Discussion
Loading comments...