0
0
MongoDBquery~20 mins

$nin for not in set in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $nin Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents where 'status' is NOT in a given set
Given a collection orders with documents containing a status field, which query returns all orders where the status is NOT 'shipped', 'delivered', or 'returned'?
MongoDB
db.orders.find({ status: { $nin: ['shipped', 'delivered', 'returned'] } })
Adb.orders.find({ status: { $not: { $in: ['shipped', 'delivered', 'returned'] } } })
Bdb.orders.find({ status: { $in: ['shipped', 'delivered', 'returned'] } })
Cdb.orders.find({ status: { $nin: ['shipped', 'delivered', 'returned'] } })
Ddb.orders.find({ status: { $ne: ['shipped', 'delivered', 'returned'] } })
Attempts:
2 left
💡 Hint
Use $nin to exclude documents where the field matches any value in the array.
🧠 Conceptual
intermediate
1:30remaining
Understanding $nin behavior with missing fields
What happens when you use $nin on a field that does not exist in some documents?
ADocuments missing the field are included only if $exists is true.
BDocuments missing the field are excluded because $nin requires the field to exist.
CDocuments missing the field cause a query error.
DDocuments missing the field are included because their value is not in the $nin array.
Attempts:
2 left
💡 Hint
Think about how MongoDB treats missing fields in queries.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in $nin usage
Which of the following MongoDB queries has a syntax error when using $nin?
Adb.collection.find({ age: { $nin: [25, 30, 35] } })
Bdb.collection.find({ age: { $nin: 25, 30, 35 } })
Cdb.collection.find({ age: { $nin: ['25', '30', '35'] } })
Ddb.collection.find({ age: { $nin: [] } })
Attempts:
2 left
💡 Hint
Check the syntax of the array passed to $nin.
optimization
advanced
2:00remaining
Optimizing a query using $nin with indexes
You have a large collection with an index on the category field. Which query is more efficient to find documents where category is NOT 'A', 'B', or 'C'?
Adb.products.find({ category: { $nin: ['A', 'B', 'C'] } })
Bdb.products.find({ category: { $ne: 'A' }, category: { $ne: 'B' }, category: { $ne: 'C' } })
Cdb.products.find({ category: { $not: { $in: ['A', 'B', 'C'] } } })
Ddb.products.find({ category: { $nin: ['A', 'B', 'C'] }, category: { $exists: true } })
Attempts:
2 left
💡 Hint
Consider how MongoDB uses indexes with $nin versus multiple $ne conditions.
🔧 Debug
expert
2:30remaining
Why does this $nin query return unexpected results?
A developer runs this query:
db.users.find({ roles: { $nin: ['admin', 'moderator'] } })
But it returns users who have 'admin' role. Why?
ABecause 'roles' is an array field, $nin checks if the entire array is not in the list, not individual elements.
BBecause $nin does not work on array fields at all.
CBecause the query syntax is invalid and MongoDB ignores $nin.
DBecause the roles field contains null values which match $nin.
Attempts:
2 left
💡 Hint
Think about how $nin behaves with array fields.