What is BSON and how does it differ from JSON?

Beginner

Answer

BSON (Binary JSON) is the binary representation of JSON-like documents that MongoDB uses internally.

Key differences:

  • Storage: BSON is binary, JSON is text
  • Data types: BSON supports additional types like ObjectId, Date, Binary
  • Performance: BSON is faster to parse and process
  • Size: BSON can be larger due to additional metadata
  • Traversal: BSON allows efficient traversal of documents

Example BSON types not in JSON:

{
  _id: ObjectId("507f1f77bcf86cd799439011"),
  created: ISODate("2023-01-01T12:00:00Z"),
  data: BinData(0, "SGVsbG8gV29ybGQ=")
}