Explain the role of BSON in MongoDB.

Instruction: Discuss what BSON is and why MongoDB uses it.

Context: The question aims to test the candidate's understanding of MongoDB's data storage and manipulation format, highlighting BSON's role in efficiency and performance.

Updated

Official answer

BSON is a binary document serialization format used by MongoDB. It represents documents and arrays with explicit types, including values JSON alone does not distinguish, such as 32/64-bit integers, dates, binary data, ObjectIds and Decimal128.

It is not simply compressed JSON. Field names and type/length metadata consume space, so BSON is not always smaller than a comparable JSON representation. Parsing and access characteristics depend on the implementation and workload; the format alone does not guarantee faster queries.

The document model makes nested structures natural, but choose embedding from access patterns and growth limits. Embed bounded data that belongs to one aggregate. Keep an unbounded comment history in separate documents with an appropriate index, rather than assuming every relationship should become an array in one parent.

Select types deliberately: use real date values for date operations and a suitable exact monetary representation when required. Check driver serialization and round trips, including precision, timezone conventions and large integer handling. Extended JSON can represent BSON-specific values in text, but ordinary JSON conversion may lose type information.

MongoDB BSON types, data modeling

Related Questions