What are the different BSON data types in MongoDB?
A clear guide to MongoDB BSON data types — String, Int32, Int64, Double, Decimal128, Date, ObjectId, Binary and more, plus when to use each.
Expected Interview Answer
BSON (Binary JSON) is the binary-encoded format MongoDB uses to store documents, and it supports typed fields such as String, Int32, Int64, Double, Decimal128, Boolean, Date, ObjectId, Array, Embedded Document, Binary Data, Null, Timestamp, and Regular Expression.
BSON extends JSON with additional types and length prefixes that make traversal fast. Beyond JSON's strings, numbers, booleans, arrays, objects and null, it adds distinct numeric types (Int32, Int64, Double, Decimal128), a native Date, ObjectId for default _id values, Binary Data for blobs, and an internal Timestamp used by replication. Each field carries a type byte, so MongoDB always knows exactly how to interpret a value and queries can filter by type using operators like $type.
- Preserves numeric precision with distinct number types
- Faster to traverse than plain text JSON thanks to length prefixes
- Native Date and ObjectId types beyond JSON
- Type-aware queries via the $type operator
- Compact binary encoding for storage and network transfer
AI Mentor Explanation
BSON types are like the labelled columns of a detailed scorecard. A raw crowd shout just says numbers, but the scorecard tags each figure precisely: runs as integers, strike rate as a decimal, the date as a real date, the player's registration as a unique ID. Because every value wears an explicit label, the statistician never mistakes an over number for a jersey number, and can filter records by exactly the kind of value they want.
Step-by-Step Explanation
Step 1
Start from JSON
BSON begins with JSON's types: string, number, boolean, array, object and null.
Step 2
Split the numbers
It separates numbers into Int32, Int64, Double and Decimal128 to preserve range and precision.
Step 3
Add native types
It introduces Date, ObjectId, Binary Data, Timestamp and Regular Expression that JSON lacks.
Step 4
Attach a type byte
Each field is stored with a type marker and length prefix for fast, unambiguous parsing.
Step 5
Query by type
Use the $type operator to match documents whose field holds a specific BSON type.
What Interviewer Expects
- Knows BSON stands for Binary JSON and extends it
- Can list the distinct numeric types and why they exist
- Mentions ObjectId, Date and Binary Data
- Understands Decimal128 for exact decimal precision
- Aware of the $type query operator
Common Mistakes
- Claiming BSON is identical to JSON
- Not distinguishing Int32, Int64, Double and Decimal128
- Storing money as Double and losing precision instead of Decimal128
- Confusing the internal Timestamp type with Date
Best Answer (HR Friendly)
“BSON is the binary format MongoDB uses to store documents. It is like JSON but with more precise labels for each value — separate types for whole numbers, decimals, dates, unique IDs and binary files — so the database always knows exactly what each field means.”
Code Example
db.products.insertOne({
name: "Widget", // String
quantity: NumberInt(42), // Int32
views: NumberLong("9000000000"), // Int64
weight: 1.5, // Double
price: NumberDecimal("19.99"), // Decimal128 (exact)
inStock: true, // Boolean
createdAt: new Date(), // Date
sku: ObjectId(), // ObjectId
tags: ["tools", "home"], // Array
meta: { color: "blue" } // Embedded Document
})
// Find documents whose price field is a Decimal128
db.products.find({ price: { $type: "decimal" } })Follow-up Questions
- Why should monetary values use Decimal128 instead of Double?
- What is stored inside an ObjectId?
- How does the $type operator help validate a schema?
- How does BSON differ from JSON in storage and speed?
MCQ Practice
1. Which BSON type best preserves exact monetary precision?
Decimal128 stores decimal values exactly, avoiding the binary floating-point rounding errors of Double.
2. What does BSON stand for?
BSON is Binary JSON, a binary-encoded superset of JSON with extra types and length prefixes.
3. Which type is MongoDB's default for the _id field?
MongoDB assigns an ObjectId to _id by default when a document has no explicit _id.
Flash Cards
What is BSON? — Binary JSON — the typed, length-prefixed binary format MongoDB uses to store documents.
Name the numeric BSON types — Int32, Int64, Double and Decimal128.
Which type for exact money? — Decimal128, which avoids floating-point rounding.
How do you filter by type? — Use the $type query operator, e.g. { field: { $type: 'decimal' } }.