0
0
MongoDBquery~20 mins

$in for matching a set in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $in Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents where a field matches any value in a set
Given a collection products with documents containing a category field, which query returns all products whose category is either 'electronics', 'books', or 'clothing'?
MongoDB
db.products.find({ category: { $in: ['electronics', 'books', 'clothing'] } })
AReturns products with category exactly matching the array ['electronics', 'books', 'clothing']
BReturns all products with category 'electronics', 'books', or 'clothing'
CReturns products with category not in 'electronics', 'books', or 'clothing'
DReturns all products regardless of category
Attempts:
2 left
💡 Hint
Think about how $in matches any value inside the array.
query_result
intermediate
2:00remaining
Using $in with numeric values
Consider a collection orders where each document has a numeric status_code. Which query returns orders with status codes 1, 3, or 5?
MongoDB
db.orders.find({ status_code: { $in: [1, 3, 5] } })
AReturns all orders regardless of status_code
BReturns orders with status_code exactly equal to the array [1, 3, 5]
CReturns orders with status_code 1, 3, or 5
DReturns orders with status_code not equal to 1, 3, or 5
Attempts:
2 left
💡 Hint
Remember $in checks if the field value is in the array.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in $in usage
Which of the following MongoDB queries has a syntax error when using $in to match the tags field?
Adb.collection.find({ tags: { $in: 'red', 'blue', 'green' } })
Bdb.collection.find({ tags: { $in: ['red', 'blue', 'green'] } })
C)} } ]'neerg' ,'eulb' ,'der'[ :ni$ { :sgat {(dnif.noitcelloc.bd
Ddb.collection.find({ tags: { $in: ['red'] } })
Attempts:
2 left
💡 Hint
Check how the array is passed to $in.
optimization
advanced
2:00remaining
Optimizing queries with $in on indexed fields
You have a large collection users with an index on the role field. Which query will best use the index to find users with roles 'admin', 'editor', or 'viewer'?
Adb.users.find({ $or: [{ role: 'admin' }, { role: 'editor' }, { role: 'viewer' }] })
Bdb.users.find({ role: { $all: ['admin', 'editor', 'viewer'] } })
Cdb.users.find({ role: { $nin: ['admin', 'editor', 'viewer'] } })
Ddb.users.find({ role: { $in: ['admin', 'editor', 'viewer'] } })
Attempts:
2 left
💡 Hint
Think about how $in uses indexes compared to $or.
🧠 Conceptual
expert
3:00remaining
Understanding $in behavior with arrays in documents
In a collection posts, each document has a field tags which is an array of strings. What does the query db.posts.find({ tags: { $in: ['mongodb', 'database'] } }) return?
ADocuments where the tags array contains at least one of 'mongodb' or 'database'
BDocuments where the tags array exactly matches ['mongodb', 'database']
CDocuments where the tags array contains both 'mongodb' and 'database'
DDocuments where the tags array contains neither 'mongodb' nor 'database'
Attempts:
2 left
💡 Hint
Remember how $in works with array fields in documents.