MEAN MongoDB Advanced Operations 4 — Questions and Answers
Question 1: Which MongoDB operator is used to perform a bitwise AND update on a field?
- $bit with 'and' (Correct answer)
- $band
- $bitAnd
- $bitmask
Correct answer: $bit with 'and'
The $bit operator with the 'and' key performs a bitwise AND update on integer fields in MongoDB.
Question 2: What does the MongoDB $facet aggregation stage allow you to do?
- Run multiple aggregation pipelines within a single stage on the same input (Correct answer)
- Create faceted search indexes
- Join two collections using a common field
- Split documents into multiple output collections
Correct answer: Run multiple aggregation pipelines within a single stage on the same input
$facet enables multi-faceted aggregations by processing multiple sub-pipelines on the same set of input documents in one pass.
Question 3: In MongoDB's change streams, what does the 'fullDocument' option set to 'updateLookup' do?
- Returns the full document after an update by performing an extra lookup (Correct answer)
- Streams only the changed fields of a document
- Disables partial update tracking
- Forces updates to always replace the full document
Correct answer: Returns the full document after an update by performing an extra lookup
Setting fullDocument to 'updateLookup' causes MongoDB to perform an additional query to return the full post-update document alongside the change event.
Question 4: Which read concern level guarantees that data has been written to a majority of replica set members before being read?
- majority (Correct answer)
- linearizable
- snapshot
- local
Correct answer: majority
The 'majority' read concern ensures that the data read has been acknowledged by a majority of replica set members, preventing rollback.
Question 5: What is the purpose of the MongoDB $bucket aggregation stage?
- Categorizes documents into user-defined ranges (buckets) based on a field value (Correct answer)
- Groups documents by a hashed value of a field
- Distributes documents across shards
- Creates histogram indexes on numeric fields
Correct answer: Categorizes documents into user-defined ranges (buckets) based on a field value
$bucket groups incoming documents into contiguous, user-specified ranges and counts (or computes) values within each range.
Question 6: Which MongoDB command reports the current in-progress operations on a mongod instance?
- db.currentOp() (Correct answer)
- db.runCommand({listOperations:1})
- db.inProgress()
- db.stats()
Correct answer: db.currentOp()
db.currentOp() returns a document containing information about active operations currently running on the mongod instance.
Question 7: What happens when you use the MongoDB $merge aggregation stage with 'whenMatched: replace'?
- Replaces the entire matching document in the target collection with the pipeline output document (Correct answer)
- Merges only the changed fields into the matching document
- Deletes the matching document and inserts a new one
- Skips the document if a match is found
Correct answer: Replaces the entire matching document in the target collection with the pipeline output document
The 'replace' option for whenMatched causes $merge to overwrite the entire existing document in the target collection.
Which MongoDB operator is used to perform a bitwise AND update on a field?