MEAN MongoDB Advanced Operations 1 — Questions and Answers
Question 1: Which MongoDB aggregation stage is used to filter documents before passing them to the next stage in the pipeline?
- $match (Correct answer)
- $filter
- $where
- $select
Correct answer: $match
The $match stage filters documents using query conditions, similar to find(), and should be placed early in the pipeline for performance.
Question 2: What does the $lookup aggregation stage do in MongoDB?
- Performs a left outer join with another collection (Correct answer)
- Creates an index on a field
- Filters duplicate documents
- Sorts documents by a field
Correct answer: Performs a left outer join with another collection
$lookup performs a left outer join between two collections in the same database, adding matched documents as an array field.
Question 3: Which MongoDB index type is best suited for geospatial queries?
- 2dsphere (Correct answer)
- Text
- Hashed
- Compound
Correct answer: 2dsphere
A 2dsphere index supports queries on GeoJSON objects and legacy coordinate pairs for geospatial calculations.
Question 4: What is the purpose of the MongoDB `explain()` method?
- Returns execution plan and performance statistics for a query (Correct answer)
- Adds a description field to documents
- Generates schema documentation
- Lists all indexes on a collection
Correct answer: Returns execution plan and performance statistics for a query
explain() provides details about query execution, including the winning plan, index usage, and statistics like documents examined.
Question 5: In MongoDB, what does a 'covered query' mean?
- A query satisfied entirely by an index without accessing documents (Correct answer)
- A query that uses the $match stage
- A query with all fields projected
- A query running inside a transaction
Correct answer: A query satisfied entirely by an index without accessing documents
A covered query is one where all fields in the query and projection are part of the index, so MongoDB never needs to read the actual documents.
Question 6: Which MongoDB write concern value ensures acknowledgment from a majority of replica set members?
- majority (Correct answer)
- 1
- 0
- all
Correct answer: majority
Write concern { w: 'majority' } ensures the primary and a majority of secondaries have confirmed the write before acknowledging success.
Which MongoDB aggregation stage is used to filter documents before passing them to the next stage in the pipeline?