0
0
MongoDBquery~20 mins

Why logical operators matter in MongoDB - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logical Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents with age greater than 25 and city 'New York'
Given a collection users with documents containing age and city, which query returns users older than 25 who live in New York?
MongoDB
db.users.find({$and: [{age: {$gt: 25}}, {city: 'New York'}]})
Adb.users.find({age: {$gt: 25}, city: 'New York'})
Bdb.users.find({$or: [{age: {$gt: 25}}, {city: 'New York'}]})
Cdb.users.find({$and: [{age: {$gt: 25}}, {city: 'New York'}]})
Ddb.users.find({age: {$gt: 25}}).find({city: 'New York'})
Attempts:
2 left
💡 Hint
Use $and to combine conditions that both must be true.
query_result
intermediate
2:00remaining
Find documents where status is 'active' or score is above 80
Which MongoDB query returns documents where status is 'active' or score is greater than 80?
MongoDB
db.records.find({$or: [{status: 'active'}, {score: {$gt: 80}}]})
Adb.records.find({status: 'active', score: {$gt: 80}})
Bdb.records.find({$or: [{status: 'active'}, {score: {$gt: 80}}]})
Cdb.records.find({$and: [{status: 'active'}, {score: {$gt: 80}}]})
Ddb.records.find({status: {$in: ['active', {$gt: 80}]}})
Attempts:
2 left
💡 Hint
Use $or to match either condition.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this MongoDB query
Which option contains a syntax error in the MongoDB query to find documents where type is 'admin' and active is true?
Adb.users.find({$and: [{type: 'admin'}, {active: true}]})
Bb.users.find({$and: [{type: 'admin'}, {active: true}]})
C)}]}eurt :evitca{ ,}'nimda' :epyt{[ :dna${(dnif.sresu.bd
Ddb.users.find({type: 'admin', active: true})
Attempts:
2 left
💡 Hint
Check for matching brackets and commas.
🧠 Conceptual
advanced
2:00remaining
Why use $nor instead of $not with $or?
Which statement best explains why $nor is preferred over using $not with $or in MongoDB queries?
A<code>$not</code> can only negate single conditions, so it cannot negate an <code>$or</code> with multiple conditions, but <code>$nor</code> can.
B<code>$nor</code> returns documents where none of the conditions are true, while <code>$not</code> cannot be applied to <code>$or</code> directly.
C<code>$nor</code> is deprecated and should be replaced with <code>$not</code> and <code>$or</code>.
D<code>$not</code> with <code>$or</code> is faster than <code>$nor</code> but less readable.
Attempts:
2 left
💡 Hint
Think about what each operator can negate.
optimization
expert
3:00remaining
Optimize query to find users not in city 'Boston' and age less than 30
Which MongoDB query is the most efficient and correct way to find users who are NOT in 'Boston' and have age less than 30?
Adb.users.find({$and: [{city: {$nin: ['Boston']}}, {age: {$lt: 30}}]})
Bdb.users.find({city: {$not: {$eq: 'Boston'}}, age: {$lt: 30}})
Cdb.users.find({$nor: [{city: 'Boston'}], age: {$lt: 30}})
Ddb.users.find({$and: [{city: {$ne: 'Boston'}}, {age: {$lt: 30}}]})
Attempts:
2 left
💡 Hint
Use operators that clearly express negation and combine conditions properly.