MEAN MongoDB Advanced Operations 2 — Questions and Answers
Question 1: What is the MongoDB $unwind aggregation stage used for?
- Deconstructs an array field into separate documents (Correct answer)
- Merges two arrays together
- Removes null fields from documents
- Converts strings to arrays
Correct answer: Deconstructs an array field into separate documents
$unwind deconstructs an array field, outputting one document per array element with the field replaced by that element's value.
Question 2: Which MongoDB feature allows you to enforce a schema on a collection?
- Schema Validation with $jsonSchema (Correct answer)
- Mongoose only
- Collection constraints
- Document triggers
Correct answer: Schema Validation with $jsonSchema
MongoDB's built-in schema validation using $jsonSchema lets you define required fields, types, and constraints that documents must satisfy.
Question 3: What does the MongoDB `upsert` option do in an update operation?
- Inserts a new document if no matching document exists (Correct answer)
- Updates all matching documents
- Deletes and re-inserts the document
- Validates the document before updating
Correct answer: Inserts a new document if no matching document exists
When upsert: true is set, MongoDB inserts a new document if no document matches the filter criteria, otherwise it updates the existing one.
Question 4: In MongoDB, what is a capped collection?
- A fixed-size collection that overwrites oldest documents when full (Correct answer)
- A collection with a maximum document size limit
- A collection with read-only access
- A collection limited to 100 documents
Correct answer: A fixed-size collection that overwrites oldest documents when full
Capped collections have a fixed size and automatically overwrite the oldest entries in insertion order when the size limit is reached.
Question 5: Which operator is used to atomically increment a numeric field in a MongoDB document?
- $inc (Correct answer)
- $add
- $increment
- $push
Correct answer: $inc
The $inc operator atomically increments (or decrements with a negative value) a numeric field by the specified amount in a single operation.
Question 6: What is MongoDB's WiredTiger storage engine's default compression algorithm for documents?
- Snappy (Correct answer)
- zlib
- zstd
- LZ4
Correct answer: Snappy
WiredTiger uses Snappy compression by default for collection data because it offers a good balance of compression ratio and CPU performance.
What is the MongoDB $unwind aggregation stage used for?