MongoDB MongoDB Data Modeling 2 — Questions and Answers
Question 1: What is the 'computed pattern' in MongoDB data modeling?
- Computing field values at query time using aggregation
- Pre-computing and storing frequently accessed calculated values in the document (Correct answer)
- Using computed columns like in relational databases
- Computing shard keys from existing fields
Correct answer: Pre-computing and storing frequently accessed calculated values in the document
The computed pattern stores the result of expensive calculations alongside the source data, avoiding repeated computation on every read.
Question 2: What is the 'attribute pattern' in MongoDB schema design?
- Listing all document attributes in a schema file
- Transforming varied fields into a consistent array of key-value pairs (Correct answer)
- Using attribute-based access control in schemas
- Defining attribute types for schema validation
Correct answer: Transforming varied fields into a consistent array of key-value pairs
The attribute pattern converts documents with many similar but not identical fields into an array of {k, v} subdocuments, making indexing more efficient.
Question 3: In MongoDB schema design, what is the 'extended reference pattern'?
- Using DBRef objects for all references
- Duplicating a subset of frequently accessed fields from the referenced document (Correct answer)
- Extending the _id field with additional metadata
- Creating bidirectional references between documents
Correct answer: Duplicating a subset of frequently accessed fields from the referenced document
The extended reference pattern stores the _id reference plus a few frequently accessed fields from the related document to reduce join-like operations.
Question 4: What is document validation in MongoDB?
- Validating that documents are syntactically correct JSON
- Enforcing schema rules on insert and update operations using validation expressions (Correct answer)
- Checking documents for duplicate _id values
- Verifying document checksums after writes
Correct answer: Enforcing schema rules on insert and update operations using validation expressions
MongoDB's document validation uses JSON Schema or query expressions to enforce rules on documents during insert and update operations.
Question 5: What is the 'tree pattern' used for in MongoDB data modeling?
- Creating B-tree index structures
- Representing hierarchical data structures like org charts or category trees (Correct answer)
- Organizing collections in a tree hierarchy
- Storing filesystem-like data structures
Correct answer: Representing hierarchical data structures like org charts or category trees
The tree pattern represents hierarchical parent-child relationships, with various sub-patterns like parent references, child references, or materialized paths.
Question 6: Which MongoDB feature enforces that fields meet specific data type requirements?
- Strict mode
- Schema validation with $jsonSchema (Correct answer)
- Type checking middleware
- Collection constraints
Correct answer: Schema validation with $jsonSchema
$jsonSchema in collection validation rules allows specifying required fields, allowed types, value ranges, and other constraints enforced on write operations.
What is the 'computed pattern' in MongoDB data modeling?