← Back to MongoDB Course | Chapter 1: Introduction | Lesson 4 of 7

BSON format

MongoDB stores documents in BSON, a binary form of JSON that adds extra types like dates, integers and ObjectId.

In this page:

  1. BSON format
Syntax
javascript
{
  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

bash
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/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming JSON dates are stored as strings
  2. Using doubles for currency
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.