MongoDB MongoDB Schema Design and Data Modeling 1 — Questions and Answers
Question 1: What is the key principle that distinguishes embedding from referencing in MongoDB schema design?
- Embedding always produces faster queries than referencing
- Embedding stores related data in the same document; referencing stores it in separate documents with links (Correct answer)
- Referencing is only for relational databases
- Embedding is required for all array fields
Correct answer: Embedding stores related data in the same document; referencing stores it in separate documents with links
Embedding stores related data within the same BSON document, enabling single-document reads. Referencing stores related data in separate documents and links them via ObjectId fields, similar to foreign keys, requiring multiple reads or $lookup for joins.
Question 2: What is the maximum BSON document size in MongoDB?
- 4 MB
- 8 MB
- 16 MB (Correct answer)
- 32 MB
Correct answer: 16 MB
MongoDB enforces a maximum BSON document size of 16 megabytes. This limit ensures that individual documents do not use excessive amounts of RAM or bandwidth. For larger data, use GridFS which splits files into chunks.
Question 3: Which data modeling pattern is best suited when a one-to-many relationship has a bounded, small set of related items that are always read together?
- Reference pattern
- Embedded document pattern (Correct answer)
- Bucket pattern
- Polymorphic pattern
Correct answer: Embedded document pattern
The embedded document pattern is optimal for one-to-many relationships where the 'many' side is bounded and small. Embedding keeps related data co-located, enabling single-document retrieval and avoiding expensive joins or multiple queries.
Question 4: What is the Outlier Pattern in MongoDB schema design used for?
- Handling documents that exceed normal field counts
- Managing a small number of documents that have many more relationships than typical documents (Correct answer)
- Separating outlier queries to a different collection
- Flagging data quality issues
Correct answer: Managing a small number of documents that have many more relationships than typical documents
The Outlier Pattern addresses the situation where a few documents have far more related items than typical, such as a popular author with thousands of books. A flag field identifies outliers, and overflow data is stored in separate documents to avoid document size limits.
Question 5: In MongoDB, what is the Bucket Pattern primarily used for?
- Grouping documents by geographic region
- Aggregating time-series or IoT data into fixed-size groups within a single document (Correct answer)
- Partitioning collections across shards
- Batching write operations
Correct answer: Aggregating time-series or IoT data into fixed-size groups within a single document
The Bucket Pattern groups a stream of data (e.g., IoT sensor readings or time-series events) into fixed-size or time-window documents. Each bucket document holds many measurements, reducing document count, improving index performance, and enabling efficient range queries.
Question 6: What is a key advantage of using the Polymorphic Pattern in MongoDB?
- It enforces strict schema validation across all documents
- It allows documents with different shapes to be stored in the same collection, simplifying queries across related entities (Correct answer)
- It prevents duplicate records across collections
- It automatically indexes all field variants
Correct answer: It allows documents with different shapes to be stored in the same collection, simplifying queries across related entities
The Polymorphic Pattern stores documents with different schemas in one collection. A discriminator field (e.g., 'type') identifies each variant. This simplifies queries that span different entity subtypes without requiring joins across multiple collections.
What is the key principle that distinguishes embedding from referencing in MongoDB schema design?