BSON adds types beyond JSON: ObjectId, Date, Decimal128, BinData, and more. Pick types for query and index behavior.
Common types
- String, Boolean, Int32/Int64, Double — scalars
- Date — UTC instant (ISODate in shell)
- ObjectId — default _id, 12-byte identifier
- Decimal128 — exact decimals (money)
- Null — explicit null vs missing field
Typed insert
db.ledger.insertOne({
amount: NumberDecimal('19.99'),
postedAt: ISODate('2026-01-15T12:00:00Z')
})Practice: Run in mongosh on your practice database.
Missing vs null
A missing field and null behave differently in queries—design APIs consistently.
Important interview questions and answers
- Q: Money as double?
A: Avoid float rounding—use Decimal128 or integer cents. - Q: ObjectId contains timestamp?
A: First 4 bytes encode creation time—rough ordering possible.
Self-check
- Which type for currency?
- Difference between missing field and null?
Tip: Use Decimal128 or integer cents for money—avoid binary float surprises.
Interview prep
- Money type?
- Decimal128 or integer cents—avoid float.
- Null vs missing?
- Different query semantics—be consistent in APIs.