BSON format
MongoDB stores documents in BSON, a binary form of JSON that adds extra types like dates, integers and ObjectId.
In this page:
Syntax
{
field: "text",
count: NumberInt(value),
created: new Date(),
_id: ObjectId()
}
BSON format
BSON (Binary JSON) is the on-disk and wire format. It keeps JSON's structure but adds types such as Date, ObjectId, Int32, Int64, Decimal128, Binary and Timestamp, and it is designed to be fast to scan.
You see it as extended JSON in tools and drivers.
Note:
Use Decimal128 for money to avoid floating-point rounding surprises.
Example: BSON format
test> db.types.insertOne({
... _id: 1,
... name: "sample",
... count: Int32(5),
... big: Long("9007199254740993"),
... price: Decimal128("19.99"),
... created: ISODate("2024-01-05T10:00:00Z")
... })
{ acknowledged: true, insertedId: 1 }
test> db.types.findOne()
{
_id: 1,
name: 'sample',
count: 5,
big: Long('9007199254740993'),
price: Decimal128('19.99'),
created: ISODate('2024-01-05T10:00:00.000Z')
}
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Assuming JSON dates are stored as strings
- Using doubles for currency
- Comparing numbers of different BSON types carelessly
Chapter Summary
- BSON is binary JSON with extra types
- Adds Date, ObjectId, Int32, Int64, Decimal128
- Fast to scan and traverse
- Shown as extended JSON in tools
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: