0
0
MongoDBquery~20 mins

$and operator behavior in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $and Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What documents does this $and query return?
Consider a collection students with documents like:
{ "name": "Alice", "age": 20, "grade": "A" }

What documents will this query return?
{ $and: [ { age: { $gt: 18 } }, { grade: "A" } ] }
MongoDB
{ $and: [ { age: { $gt: 18 } }, { grade: "A" } ] }
ADocuments where age is greater than 18 or grade is 'A'
BDocuments where age is greater than 18 and grade is 'A'
CDocuments where age is less than or equal to 18 and grade is 'A'
DDocuments where age is greater than 18 but grade is not 'A'
Attempts:
2 left
💡 Hint
Remember $and requires all conditions to be true for a document.
📝 Syntax
intermediate
1:30remaining
Which $and query syntax is correct?
Choose the syntactically valid MongoDB query using $and to find documents with age 25 and city 'NY'.
A{ $and: ( { age: 25 }, { city: 'NY' } ) }
B{ $and: { age: 25, city: 'NY' } }
C{ $and: [ age: 25, city: 'NY' ] }
D{ $and: [ { age: 25 }, { city: 'NY' } ] }
Attempts:
2 left
💡 Hint
Check the data structure used with $and: it must be an array of conditions.
optimization
advanced
2:00remaining
Which query is more efficient to find documents with age 30 and status 'active'?
Given a collection with indexes on age and status, which query is better for performance?
A{ $or: [ { age: 30 }, { status: 'active' } ] }
B{ $and: [ { age: 30 }, { status: 'active' } ] }
C{ age: 30, status: 'active' }
D{ $and: { age: 30, status: 'active' } }
Attempts:
2 left
💡 Hint
MongoDB treats multiple fields in a query as an implicit AND.
🔧 Debug
advanced
2:30remaining
Why does this $and query return no results?
Given documents with fields 'score' and 'passed', why does this query return no documents?
{ $and: [ { score: { $gt: 50 } }, { score: { $lt: 40 } } ] }
MongoDB
{ $and: [ { score: { $gt: 50 } }, { score: { $lt: 40 } } ] }
ANo document can have score greater than 50 and less than 40 at the same time
BThe syntax of $and is incorrect
CThe fields 'score' and 'passed' are missing in documents
DThe query should use $or instead of $and
Attempts:
2 left
💡 Hint
Think about the logic of the conditions combined with $and.
🧠 Conceptual
expert
3:00remaining
How does MongoDB internally treat multiple conditions combined with $and?
Which statement best describes how MongoDB processes queries with $and operator?
AMongoDB evaluates each condition separately and returns documents that satisfy all conditions, using indexes if available
BMongoDB converts $and queries into $or queries internally for optimization
CMongoDB only uses the first condition in the $and array and ignores the rest
DMongoDB merges all conditions into a single condition and evaluates it once
Attempts:
2 left
💡 Hint
Think about how multiple filters work together and how indexes help.