Challenge - 5 Problems
MongoDB $all Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find documents where the 'tags' array contains both 'red' and 'blue'
Given a collection with documents containing a 'tags' array, which query returns documents where 'tags' contains both 'red' and 'blue'?
MongoDB
db.collection.find({ tags: { $all: ['red', 'blue'] } })Attempts:
2 left
💡 Hint
Use $all to match arrays containing all specified elements.
✗ Incorrect
The $all operator matches arrays that contain all the specified elements. Option A correctly uses $all with both 'red' and 'blue'. Option A uses $in which matches any of the elements, not all. Options C and D only check for one element each.
🧠 Conceptual
intermediate2:00remaining
Understanding $all operator behavior with nested arrays
What does the $all operator do when matching documents where the field contains nested arrays? For example, if a document has field 'values': [[1,2], [3,4]], what does $all: [[1,2], [3,4]] match?
Attempts:
2 left
💡 Hint
Think about how $all matches elements exactly, including arrays.
✗ Incorrect
The $all operator matches elements exactly. So if you specify $all: [[1,2], [3,4]], it looks for the array field to contain both arrays [1,2] and [3,4] as elements. It does not flatten or partially match numbers inside nested arrays.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this $all query
Which option contains a syntax error in the MongoDB query using $all to match 'colors' containing 'green' and 'yellow'?
MongoDB
db.collection.find({ colors: { $all: 'green', 'yellow' } })Attempts:
2 left
💡 Hint
Check the syntax for $all operator's value type.
✗ Incorrect
The $all operator requires an array as its value. Option D incorrectly provides multiple arguments instead of a single array, causing a syntax error. Options A, B, and C correctly use arrays.
❓ optimization
advanced2:00remaining
Optimizing a query using $all with multiple elements
You want to find documents where the 'features' array contains 'wifi', 'parking', and 'pool'. Which query is the most efficient and correct?
Attempts:
2 left
💡 Hint
Use a single query operator that matches all elements at once.
✗ Incorrect
Option C uses $all to match all specified elements in one query, which is efficient. Option C matches any of the elements, not all. Option C is invalid chaining of find(). Option C works but is less efficient than $all.
🔧 Debug
expert3:00remaining
Why does this $all query return no results?
You run this query: db.collection.find({ tags: { $all: ['apple', 'banana'] } }) but get no results, even though documents have 'tags' arrays with 'apple' and 'banana' but in nested arrays like ['apple', ['banana']] or [['apple'], 'banana']. Why?
Attempts:
2 left
💡 Hint
Consider how $all matches elements exactly, including nested arrays.
✗ Incorrect
The $all operator matches elements exactly. If the array contains nested arrays, the elements inside those nested arrays are not considered top-level elements. So 'banana' inside ['banana'] is not matched as 'banana' by $all.