Bird
Raised Fist0
MongoDBquery~20 mins

Arrays in documents in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using arrays in MongoDB documents?
easy
A. To index documents automatically
B. To create multiple documents at once
C. To enforce data types on fields
D. To store multiple values in a single field

Solution

  1. Step 1: Understand what arrays do in MongoDB

    Arrays allow storing multiple values inside one document field, like a list.
  2. Step 2: Compare options with array purpose

    Only To store multiple values in a single field correctly describes storing multiple values in one field.
  3. Final Answer:

    To store multiple values in a single field -> Option D
  4. Quick Check:

    Arrays = multiple values in one field [OK]
Hint: Arrays hold many values in one field [OK]
Common Mistakes:
  • Thinking arrays create multiple documents
  • Confusing arrays with indexing
  • Believing arrays enforce data types
2. Which of the following is the correct way to define an array field named tags in a MongoDB document?
easy
A. { tags: "mongodb, database" }
B. { tags: { "mongodb", "database" } }
C. { tags: ["mongodb", "database"] }
D. { tags: ("mongodb", "database") }

Solution

  1. Step 1: Recall MongoDB array syntax

    Arrays in MongoDB are defined using square brackets [] with comma-separated values.
  2. Step 2: Check each option's syntax

    { tags: ["mongodb", "database"] } uses square brackets correctly. Options A, B, and D use incorrect syntax for arrays.
  3. Final Answer:

    { tags: ["mongodb", "database"] } -> Option C
  4. Quick Check:

    Arrays use [] brackets [OK]
Hint: Arrays use square brackets [] in MongoDB [OK]
Common Mistakes:
  • Using quotes instead of brackets for arrays
  • Using curly braces {} which define objects
  • Using parentheses () which are invalid for arrays
3. Given the document { name: "Alice", scores: [85, 90, 78] }, what will the query db.collection.find({ scores: 90 }) return?
medium
A. Documents where the scores array contains 90
B. Documents where scores equals exactly 90
C. Documents where scores is greater than 90
D. No documents because 90 is inside an array

Solution

  1. Step 1: Understand MongoDB array matching

    Querying with { scores: 90 } matches documents where the array contains the value 90.
  2. Step 2: Analyze the given document and query

    The scores array includes 90, so the document matches and will be returned.
  3. Final Answer:

    Documents where the scores array contains 90 -> Option A
  4. Quick Check:

    Query matches array elements directly [OK]
Hint: Query value matches any array element [OK]
Common Mistakes:
  • Thinking query matches whole array only
  • Assuming query checks for greater than
  • Believing arrays block direct value matching
4. What is wrong with this MongoDB update query to add a new tag to the tags array?
db.collection.updateOne({ _id: 1 }, { $push: { tags: "new" } })
medium
A. The query is missing the filter document
B. The $push operator is used correctly; no error
C. The $push operator requires the value to be an array
D. The update document should use $addToSet instead of $push

Solution

  1. Step 1: Understand $push operator usage

    $push adds a single value to an array field; it accepts a single value, not necessarily an array.
  2. Step 2: Check the query structure

    The filter {_id: 1} is present, and $push is used correctly to add "new" to tags array.
  3. Final Answer:

    The $push operator is used correctly; no error -> Option B
  4. Quick Check:

    $push adds single values to arrays [OK]
Hint: $push accepts single values, no array needed [OK]
Common Mistakes:
  • Thinking $push needs an array value
  • Confusing $push with $addToSet for uniqueness
  • Missing the filter document in update
5. You have documents with a field comments which is an array of objects like { user: "Bob", text: "Nice!" }. How do you write a query to find documents where comments contains an object with user equal to "Bob" and text containing the word "Nice"?
hard
A. { comments: { $elemMatch: { user: "Bob", text: /Nice/ } } }
B. { "comments.user": "Bob", "comments.text": /Nice/ }
C. { comments: { $all: [ { user: "Bob" }, { text: /Nice/ } ] } }
D. { comments: { $in: [ { user: "Bob", text: /Nice/ } ] } }

Solution

  1. Step 1: Understand matching objects inside arrays

    $elemMatch matches array elements that satisfy all conditions inside it.
  2. Step 2: Analyze each option for correct syntax

    { comments: { $elemMatch: { user: "Bob", text: /Nice/ } } } uses $elemMatch with both conditions together, correctly matching one object with user "Bob" and text matching /Nice/.
  3. Final Answer:

    { comments: { $elemMatch: { user: "Bob", text: /Nice/ } } } -> Option A
  4. Quick Check:

    $elemMatch matches array elements with multiple conditions [OK]
Hint: Use $elemMatch for multiple conditions on one array element [OK]
Common Mistakes:
  • Using separate field queries (dot notation) which match conditions across different elements
  • Using $all which matches separate elements, not one
  • Using $in which matches exact elements, not partial fields