MongoDB Queries Questions with Answers

35 practice questions on MongoDB Queries (NoSQL & MongoDB) with answers and step-by-step solutions. Try 10 samples below, then practise all 35 free in the AptiNest app.

Sample questions

Question 1Easy

Which update operator changes the value of a field without replacing the entire document?

  1. $set
  2. $replace
  3. $put
  4. $change

Answer: A. $set

Explanation

$set updates only the named fields.

Step-by-step solution

  1. Use updateOne with filter.
  2. Second argument uses $set.

Why the other options are wrong

  • B. $replace is not an update operator; it does not change one field.
  • C. $put is not a MongoDB update operator.
  • D. $change is not an update operator in MongoDB.

Question 2Easy

Which operator matches documents where a field equals any value in a given list?

  1. $all
  2. $or
  3. $in
  4. $eq

Answer: C. $in

Explanation

{field: {$in: [a, b, c]}} matches if the field equals any listed value.

Step-by-step solution

  1. Need membership in a list.
  2. $in does it.

Why the other options are wrong

  • A. $all requires every listed value in an array field.
  • B. $or combines whole conditions.
  • D. $eq matches one value.

Question 3Easy

Which method returns all documents of the collection users?

  1. db.users.get()
  2. db.users.find()
  3. db.users.select()
  4. db.users.all()

Answer: B. db.users.find()

Explanation

find() with no filter matches every document.

Step-by-step solution

  1. find() returns all documents when no filter is used.
  2. db.users.find() is the correct syntax for this.
  3. Other options are not valid MongoDB methods.
  4. So, db.users.find() returns all documents of the collection users.
  5. The final answer is db.users.find().

Why the other options are wrong

  • A. get is not a MongoDB collection method; reads use find().
  • C. select is an SQL keyword, and MongoDB uses find() for reads.
  • D. all is not a MongoDB shell method; find() with no filter returns every document.

Tip: Remember: find() with no arguments returns all documents in MongoDB.

Question 4Easy

Which method deletes the first document that matches a filter?

  1. removeAll()
  2. dropOne()
  3. clearOne()
  4. deleteOne()

Answer: D. deleteOne()

Explanation

deleteOne removes at most one matching document; deleteMany removes all matches.

Step-by-step solution

  1. MongoDB has deleteOne to remove the first document that matches a filter.
  2. DeleteMany removes all matches; deleteOne removes only one.
  3. So the answer is option D, deleteOne.

Why the other options are wrong

  • A. RemoveAll is not a MongoDB method for a single match.
  • B. DropOne is not a MongoDB method; drop removes a collection.
  • C. ClearOne is not a MongoDB method.

Tip: DeleteOne removes one match; deleteMany removes all matches; drop removes a whole collection.

Question 5Medium

Which query finds documents where either age < 18 or age > 60?

  1. find({$or: [{age: {$lt: 18}}, {age: {$gt: 60}}]})
  2. find({age: {$lt: 18, $gt: 60}})
  3. find({age: {$lt: 18} || {$gt: 60}})
  4. find({$and: [{age: {$lt: 18}}, {age: {$gt: 60}}]})

Answer: A. find({$or: [{age: {$lt: 18}}, {age: {$gt: 60}}]})

Explanation

$or takes an array of conditions; any may match.

Step-by-step solution

  1. Two alternatives.
  2. Use $or.

Why the other options are wrong

  • B. Both bounds in one object means AND, impossible here.
  • C. || is not valid in a query document.
  • D. $and would require both.

Question 6Medium

What does the following update do?

db.users.updateOne({name:'Ravi'}, {$inc: {score: 5}})
  1. Replaces score with the string '5' for every user whose name is Ravi in the collection
  2. Adds 5 to score of the first matching user (sets 5 if score is absent)
  3. Subtracts 5 from score
  4. Fails because $inc needs updateMany

Answer: B. Adds 5 to score of the first matching user (sets 5 if score is absent)

Explanation

$inc adds to a numeric field and creates it with that value if missing. updateOne touches only the first match.

Step-by-step solution

  1. Filter finds the first Ravi.
  2. $inc adds 5.
  3. Missing field starts at 0.

Why the other options are wrong

  • A. updateOne only affects one document and $inc is numeric.
  • C. Negative values subtract; 5 adds.
  • D. $inc works with updateOne.

Question 7Medium

Which operator matches array documents containing all of the values 'a' and 'b' in the field tags?

  1. {tags: {$all: ['a','b']}}
  2. {tags: {$in: ['a','b']}}
  3. {tags: ['a','b']} for any order and extra elements
  4. {tags: {$size: 2}}

Answer: A. {tags: {$all: ['a','b']}}

Explanation

$all requires every listed value to be present, in any order and with extras allowed.

Step-by-step solution

  1. Both values must exist.
  2. $all.

Why the other options are wrong

  • B. $in matches any one of the values, not all of them.
  • C. A literal array needs an exact match in order.
  • D. $size checks length only.

Question 8Medium

What is the difference between find({}) and findOne({})?

  1. findOne is faster because it always uses an index, while find scans everything
  2. find returns one document and findOne returns many
  3. findOne returns a single document or null; find returns a cursor
  4. They are identical

Answer: C. findOne returns a single document or null; find returns a cursor

Explanation

findOne gives the first matching document directly, while find gives a cursor that can iterate all matches.

Step-by-step solution

  1. Return types differ.
  2. Cursor vs single document.

Why the other options are wrong

  • A. Speed is not the defining difference.
  • B. find returns many documents; findOne returns only one.
  • D. They differ: findOne returns one document, find returns a cursor.

Question 9Hard

Which document does {tags: {$all: ['a','b']}} match?

  1. {tags: ['a','c']}
  2. {tags: 'a'}
  3. {tags: ['b','c','a']}
  4. {tags: ['b']}

Answer: C. {tags: ['b','c','a']}

Explanation

$all requires the array to contain every listed element, in any order.

Step-by-step solution

  1. The query uses $all which means all elements must be present.
  2. ['a','b'] must be in the tags array.
  3. Option C has ['b','c','a'] which includes 'a' and 'b'.
  4. Other options miss at least one required element.
  5. Final answer: {tags: ['b','c','a']}.

Why the other options are wrong

  • A. Element c is present but b is missing, so $all fails.
  • B. A plain string is not an array with both a and b, so $all does not match it.
  • D. Only b is present; a is missing, so $all needs both values.

Tip: Use $all to ensure all listed items are present in an array.

Question 10Hard

Given {_id:1, a:[1,2,3]}, what does db.c.find({a: {$gt: 2, $lt: 3}}) return?

  1. No documents, because no single element is both above 2 and below 3
  2. The document, because one element is >2 (3) and another is <3 (1 or 2)
  3. An error because two operators cannot share a field
  4. Only if a has exactly one element that satisfies both

Answer: B. The document, because one element is >2 (3) and another is <3 (1 or 2)

Explanation

Without $elemMatch, each condition may be satisfied by a different array element.

Step-by-step solution

  1. 3 > 2 satisfies $gt.
  2. 1 < 3 satisfies $lt.
  3. Different elements allowed.

Why the other options are wrong

  • A. Cross-element matching makes it match.
  • C. Two operators on one field are valid and do not throw an error.
  • D. Array length not required.

Tip: Use $elemMatch to bind conditions to one element.

Practise all 35 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all NoSQL & MongoDB topics · All topics