Documents and data modeling
Syllabus
Detailed content
Documents, collections, and BSON
MongoDB stores BSON documents in collections. Documents can contain nested objects and arrays, making related data convenient to retrieve together. BSON supports typed values such as dates and binary data. Choose fields and types deliberately, and keep document size and shape predictable for the application.
Embedding versus referencing
Embed data when it is read and updated with its parent and has bounded growth. Reference data when it is shared, large, or independently updated. This decision shapes query count, document size, and update complexity. Model around real access patterns, while preserving a clear source of truth for shared entities.
Schema validation and evolution
Flexible documents do not mean schema-free applications. Define required fields, types, and allowed values with validation rules. When a model changes, plan how old documents will be read or migrated. Use version fields or compatible defaults when a rolling deployment must support more than one document shape.