MongoDB MongoDB Schema Design and Data Modeling 3 — Questions and Answers
Question 1: What is the primary trade-off when using the Subset Pattern in MongoDB schema design?
- Reduced document size at the cost of requiring more queries for complete data (Correct answer)
- Faster writes at the cost of slower indexes
- Better aggregations at the cost of larger documents
- Improved sharding at the cost of schema flexibility
Correct answer: Reduced document size at the cost of requiring more queries for complete data
The Subset Pattern splits a document into a 'hot' subset (frequently accessed fields in the main document) and a 'cold' subset (rarely accessed fields in a separate document). This reduces working set size but requires an additional query to access full data.
Question 2: In MongoDB, what does schema validation using $jsonSchema allow you to enforce?
- Only string field validation
- Required fields, field types, value ranges, and custom validation rules at the database level (Correct answer)
- Index creation on validated fields only
- Automatic document versioning
Correct answer: Required fields, field types, value ranges, and custom validation rules at the database level
MongoDB's $jsonSchema validator enforces document structure at the database level. You can specify required fields, BSON types for fields, minimum/maximum values, string patterns, and more. Validation can be set to 'error' (reject invalid docs) or 'warn' mode.
Question 3: When modeling a many-to-many relationship in MongoDB, which approach avoids duplication while keeping queries efficient?
- Embed all related documents on both sides
- Use an intermediate linking collection or store arrays of references on one or both sides (Correct answer)
- Denormalize all data into a single document
- Use a separate database for each side of the relationship
Correct answer: Use an intermediate linking collection or store arrays of references on one or both sides
For many-to-many relationships, a common approach is to store arrays of ObjectId references in documents on one or both sides. For complex cases, an intermediate collection (junction collection) explicitly models the relationship and can carry relationship-specific attributes.
Question 4: What is a key indicator that you should move from embedding to referencing in an existing schema?
- The collection has fewer than 1,000 documents
- Embedded arrays are growing unboundedly, risking the 16 MB document limit (Correct answer)
- Query response times are under 1 ms
- The database is using WiredTiger storage engine
Correct answer: Embedded arrays are growing unboundedly, risking the 16 MB document limit
When embedded arrays grow without bound — e.g., an activity log embedded in a user document — the document can eventually exceed the 16 MB BSON limit. This is a clear signal to switch to referencing, storing related records in a separate collection.
Question 5: Which field is automatically created by MongoDB for every document if not explicitly provided?
- id
- _id (Correct answer)
- objectId
- documentId
Correct answer: _id
MongoDB automatically creates an _id field for every document if one is not provided. By default, this is an ObjectId — a 12-byte BSON type that encodes a timestamp, machine identifier, process ID, and a random increment, ensuring global uniqueness.
Question 6: What does the Anti-Pattern of using MongoDB as a key-value store with large arbitrary JSON blobs typically cause?
- Improved query performance due to flexible schemas
- Inability to use indexes, poor query performance, and difficulty filtering on nested data (Correct answer)
- Automatic sharding of large documents
- Faster replication due to reduced field count
Correct answer: Inability to use indexes, poor query performance, and difficulty filtering on nested data
Storing arbitrary JSON blobs without a defined schema means MongoDB cannot create effective indexes on nested fields. Queries require scanning entire documents, indexes cannot be leveraged, and the application must parse large payloads client-side — a common anti-pattern.
What is the primary trade-off when using the Subset Pattern in MongoDB schema design?