Challenge - 5 Problems
Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find documents with array containing a specific value
Given a collection
Assume the collection contains:
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" })Attempts:
2 left
💡 Hint
The query matches documents where the array contains the exact value.
✗ Incorrect
The query matches documents where the 'tags' array contains the string 'organic'. Documents 1 and 3 have 'organic' in their tags, so they are returned.
❓ query_result
intermediate2:00remaining
Query documents with array length condition
What documents will this query return?
Assuming the collection
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 } })Attempts:
2 left
💡 Hint
The $size operator matches arrays with exactly the specified length.
✗ Incorrect
Only document 1 has an 'items' array with exactly 3 elements, so only it is returned.
📝 Syntax
advanced2:00remaining
Identify the syntax error in array update query
Which option contains a syntax error when trying to add a new element to the
Query intent: Add "sale" to the tags array for document with
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" } })Attempts:
2 left
💡 Hint
Check the operator used inside $push.
✗ Incorrect
Option A uses an invalid operator '$add' inside $push, which causes a syntax error. The correct operator to add elements is $each or directly pushing a value.
❓ query_result
advanced2:00remaining
Find documents where array contains all specified elements
What documents will this query return?
Given the collection
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"] } })Attempts:
2 left
💡 Hint
The $all operator matches arrays containing all specified elements, regardless of order.
✗ Incorrect
Documents 1 and 3 both have 'flour' and 'sugar' in their ingredients arrays, so they match the query.
🧠 Conceptual
expert2:00remaining
Understanding atomicity of array updates in MongoDB
Which statement about updating arrays in MongoDB is true?
Attempts:
2 left
💡 Hint
Think about MongoDB's document-level atomicity.
✗ Incorrect
MongoDB guarantees atomicity at the single document level, so all updates to arrays within one document happen atomically.