R
Rishtaara
MongoDB Fundamentals
Lesson 8 of 40Article16 min

JSON, BSON & Data Types

JSON (JavaScript Object Notation) is a text format for objects and arrays. Keys are strings; values can be strings, numbers, booleans, null, arrays, or nested objects. APIs and browsers use JSON constantly.

Introduction to JSON

JSON (JavaScript Object Notation) is a text format for objects and arrays. Keys are strings; values can be strings, numbers, booleans, null, arrays, or nested objects. APIs and browsers use JSON constantly.

MongoDB documents look like JSON but are stored as BSON on disk — richer types and faster parsing than plain text JSON.

Real-life example: JSON is a handwritten note anyone can read. BSON is the same note scanned into a structured digital form — machines read it faster and store extra metadata.

  • Object — { "key": "value" }
  • Array — [1, 2, "three"]
  • JSON has no Date or binary type — BSON adds those
  • Valid JSON requires double quotes on keys and strings
Valid JSON example
{
  "name": "Rahul",
  "scores": [88, 92, 79],
  "passed": true,
  "address": {
    "city": "Delhi",
    "pincode": "110001"
  }
}

What is BSON and why binary?

BSON (Binary JSON) extends JSON with additional types: Date, ObjectId, int32/int64, Decimal128, Binary, Timestamp, and more. Field names are stored with length prefixes so parsing skips scanning.

Binary storage is compact and fast to traverse — important when millions of documents live on disk. Drivers convert between BSON and native language types (JavaScript objects, Python dicts).

Real-life example: JSON is a paperback — human-friendly. BSON is the hardcover library binding — slightly heavier to produce, but libraries (databases) shelve and retrieve it efficiently at scale.

  • BSON is a binary encoding — not meant for humans to read raw
  • Drivers handle BSON ↔ language objects automatically
  • Extra types cover real app needs — dates, decimals, unique IDs
  • Document limit 16 MB — large files use GridFS

Common BSON types in MongoDB

Know these types when writing queries and designing schemas. Wrong type assumptions (string "42" vs number 42) cause filters to miss documents.

Real-life example: BSON types are ingredient labels — flour vs sugar look similar in a jar, but using the wrong one ruins the recipe (query).

  • String — UTF-8 text (names, emails, slugs)
  • Number — int32, int64, double, Decimal128 (use Decimal128 for money when needed)
  • Boolean — true / false
  • Date — UTC datetime (ISODate in shell)
  • Array — ordered list of values
  • Object / Embedded document — nested key-value object
  • ObjectId — 12-byte identifier for _id
  • Null — explicit null value
  • Binary — raw bytes (images, files metadata)
  • Regular Expression — pattern matching in queries
Tip: In JavaScript drivers, new Date() becomes BSON Date. Plain JSON from fetch() has ISO date strings — convert before insert if you need Date type queries.
Document using multiple BSON types
db.products.insertOne({
  name: "Wireless Mouse",           // String
  price: NumberDecimal("899.00"),   // Decimal128 for exact money
  stock: 42,                        // Number (int/double)
  inStock: true,                    // Boolean
  tags: ["wireless", "peripheral"], // Array
  specs: { dpi: 1600, wireless: true }, // Embedded document
  createdAt: new Date(),            // Date
  notes: null                       // Null
})
Query by type with $type
db.products.find({ price: { $type: "decimal" } })
db.products.find({ stock: { $type: "int" } })