MongoDB MongoDB CRUD Operations 2 — Questions and Answers
Question 1: Which query operator selects documents where the field value is within a specified array?
- $in (Correct answer)
- $all
- $elemMatch
- $exists
Correct answer: $in
$in matches documents where the field's value equals any value in the provided array.
Question 2: What does the $push operator do in MongoDB?
- Removes an element from an array
- Appends a value to an array field (Correct answer)
- Pushes a document to a new collection
- Creates a new index
Correct answer: Appends a value to an array field
$push appends a specified value to an array field, creating the array if it does not exist.
Question 3: Which method is used to retrieve all documents matching a query in MongoDB?
- findAll()
- find() (Correct answer)
- getAll()
- query()
Correct answer: find()
find() returns a cursor to all documents matching the query filter, or all documents if no filter is provided.
Question 4: How do you limit the number of documents returned by a query in MongoDB?
- find().max(5)
- find().restrict(5)
- find().limit(5) (Correct answer)
- find().top(5)
Correct answer: find().limit(5)
The limit() cursor method restricts the number of documents returned to the specified count.
Question 5: What operator is used to match documents where a field value is NOT equal to a specified value?
- $not
- $ne (Correct answer)
- $nin
- $neq
Correct answer: $ne
$ne (not equal) selects documents where the field value is not equal to the specified value, including documents without that field.
Question 6: Which update operator removes a field from a document?
- $remove
- $delete
- $unset (Correct answer)
- $drop
Correct answer: $unset
$unset removes the specified field from a document, and if the field does not exist, the operation is a no-op.
Which query operator selects documents where the field value is within a specified array?