Finding Documents by Field Value
What is the correct MongoDB query to find all documents where the 'status' field is equal to 'active'?
- { status: 'active' }
- db.collection.find({ status: 'active' })
- db.collection.find('status': 'active')
- db.collection.findOne({ status: 'active' })
- {'status': 'actiev'}
Using the $gt Operator
Which operator is used in MongoDB to find documents where a field's value is greater than a specified value?
- $lt
- $gte
- $eq
- $gt
- $lte
Filtering with $in
How do you find documents where the 'category' field is either 'books' or 'movies' using the $in operator?
- db.collection.find({ category: { $in: ['books', 'movies'] } })
- db.collection.find({ category: ['$in', 'books', 'movies'] })
- db.collection.find({ category: { $eq: ['books', 'movies'] } })
- db.collection.find({ category: { $all: ['books', 'movies'] } })
- db.collection.find({ category: { $in: 'books', 'movies' } })
Using $and Operator
What is the correct way to use the $and operator to find documents where 'age' is greater than 25 AND 'city' is 'New York'?
- db.collection.find({ $and: [ { age: { $gt: 25 } }, { city: 'New York' } ] })
- db.collection.find({ age: { $gt: 25 }, city: 'New York' })
- db.collection.find({ $and: { age: { $gt: 25 }, city: 'New York' } })
- db.collection.find({ age: $gt(25) AND city: 'New York' })
- db.collection.find({ $and: [ age: { $gt: 25 }, city: 'New York' ] })
The $exists Operator
How would you find documents where the field 'email' exists?
- db.collection.find({ email: { $exists: true } })
- db.collection.find({ email: { $exists: 1 } })
- db.collection.find({ email: $exists(true) })
- db.collection.find({ $exists: 'email' })
- db.collection.find({ email: { $exists: 'true' } })
Filtering Arrays with $elemMatch
Which operator is used to filter documents where an array field contains at least one element that matches all specified criteria?
- $all
- $in
- $elemMatch
- $size
- $contains
Using $not with $regex
How do you find documents where the 'name' field does NOT contain the word 'test' using $not and $regex?
- db.collection.find({ name: { $not: /test/ } })
- db.collection.find({ name: { $regex: 'test', $not: true } })
- db.collection.find({ name: { $not: { $regex: 'test' } } })
- db.collection.find({ $not: { name: { $regex: 'test' } } })
- db.collection.find({name: {$regex: '^((?!test).)*$'}})
Limiting and Skipping Results
How would you retrieve the first 5 documents and skip the initial 10 in a collection, using MongoDB's limit and skip methods?
- db.collection.find().limit(5).skip(10)
- db.collection.find().skip(10).limit(5)
- db.collection.limit(5).skip(10).find()
- db.collection.find().limit(10).skip(5)
- db.collection.find().skip(5).limit(10)
Querying with Regular Expressions
What is the correct syntax to find documents where the 'city' field starts with the letter 'N' using a regular expression?
- db.collection.find({ city: /^N/ })
- db.collection.find({ city: { $regex: 'N*' } })
- db.collection.find({ city: { $startswith: 'N' } })
- db.collection.find({ city: { $regex: 'N' } })
- db.collection.find({ city: {$regex: '^[N]'}})
Using $or Operator
How to find documents where either 'quantity' is less than 10 OR 'price' is greater than 100?
- db.collection.find({ $or: [ { quantity: { $lt: 10 } }, { price: { $gt: 100 } } ] })
- db.collection.find({ quantity: { $lt: 10 } || price: { $gt: 100 } })
- db.collection.find({ $or: { quantity: { $lt: 10 }, price: { $gt: 100 } } })
- db.collection.find({ quantity: { $lt: 10 }, $or: { price: { $gt: 100 } } })
- db.collection.find({ or: [ { quantity: { $lt: 10 } }, { price: { $gt: 100 } } ] })