0
0
MongoDBquery~20 mins

Arrays in documents in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents with array containing a specific value
Given a collection products where each document has an array field tags, what is the result of this query?

db.products.find({ tags: "organic" })

Assume the collection contains:
{ _id: 1, tags: ["organic", "fresh"] }
{ _id: 2, tags: ["imported", "fresh"] }
{ _id: 3, tags: ["organic", "local"] }
MongoDB
db.products.find({ tags: "organic" })
A[{ _id: 1, tags: ["organic", "fresh"] }, { _id: 3, tags: ["organic", "local"] }]
B[{ _id: 2, tags: ["imported", "fresh"] }]
C[]
D[{ _id: 1, tags: ["organic", "fresh"] }, { _id: 2, tags: ["imported", "fresh"] }, { _id: 3, tags: ["organic", "local"] }]
Attempts:
2 left
💡 Hint
The query matches documents where the array contains the exact value.
query_result
intermediate
2:00remaining
Query documents with array length condition
What documents will this query return?

db.orders.find({ items: { $size: 3 } })

Assuming the collection orders has:
{ _id: 1, items: ["apple", "banana", "orange"] }
{ _id: 2, items: ["apple"] }
{ _id: 3, items: ["banana", "orange", "grape", "pear"] }
MongoDB
db.orders.find({ items: { $size: 3 } })
A[{ _id: 2, items: ["apple"] }]
B[{ _id: 1, items: ["apple", "banana", "orange"] }]
C[{ _id: 3, items: ["banana", "orange", "grape", "pear"] }]
D[{ _id: 1, items: ["apple", "banana", "orange"] }, { _id: 3, items: ["banana", "orange", "grape", "pear"] }]
Attempts:
2 left
💡 Hint
The $size operator matches arrays with exactly the specified length.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in array update query
Which option contains a syntax error when trying to add a new element to the tags array in a document?

Query intent: Add "sale" to the tags array for document with _id: 5.
MongoDB
db.products.updateOne({ _id: 5 }, { $push: { tags: "sale" } })
Adb.products.updateOne({ _id: 5 }, { $push: { tags: { $add: "sale" } } })
Bdb.products.updateOne({ _id: 5 }, { $push: { tags: ["sale"] } })
Cdb.products.updateOne({ _id: 5 }, { $push: { tags: { $each: ["sale"] } } })
Ddb.products.updateOne({ _id: 5 }, { $push: { tags: "sale" } })
Attempts:
2 left
💡 Hint
Check the operator used inside $push.
query_result
advanced
2:00remaining
Find documents where array contains all specified elements
What documents will this query return?

db.recipes.find({ ingredients: { $all: ["flour", "sugar"] } })

Given the collection recipes:
{ _id: 1, ingredients: ["flour", "sugar", "eggs"] }
{ _id: 2, ingredients: ["flour", "butter"] }
{ _id: 3, ingredients: ["sugar", "flour"] }
MongoDB
db.recipes.find({ ingredients: { $all: ["flour", "sugar"] } })
A[]
B[{ _id: 2, ingredients: ["flour", "butter"] }]
C[{ _id: 1, ingredients: ["flour", "sugar", "eggs"] }]
D[{ _id: 1, ingredients: ["flour", "sugar", "eggs"] }, { _id: 3, ingredients: ["sugar", "flour"] }]
Attempts:
2 left
💡 Hint
The $all operator matches arrays containing all specified elements, regardless of order.
🧠 Conceptual
expert
2:00remaining
Understanding atomicity of array updates in MongoDB
Which statement about updating arrays in MongoDB is true?
AMongoDB does not support atomic updates on arrays; each element update is separate and can partially fail.
BArray updates are atomic only if the array has less than 10 elements.
CUpdating multiple elements in an array within a single document is atomic; either all changes succeed or none do.
DAtomicity applies only when updating arrays in multiple documents, not within a single document.
Attempts:
2 left
💡 Hint
Think about MongoDB's document-level atomicity.