Arrays let you store multiple values inside one document. This helps keep related data together in one place.
Arrays in documents in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
{
"fieldName": [value1, value2, value3, ...]
}Arrays are written inside square brackets [].
Values inside arrays can be any type: strings, numbers, objects, or even other arrays.
Examples
MongoDB
{ "tags": ["mongodb", "database", "nosql"] }MongoDB
{ "scores": [85, 90, 78] }MongoDB
{ "contacts": [{ "type": "email", "value": "a@example.com" }, { "type": "phone", "value": "123-456" }] }MongoDB
{ "emptyArray": [] }Sample Program
This example adds a user with a 'hobbies' array. Then it finds and prints the user document.
MongoDB
db.users.insertOne({
name: "Alice",
hobbies: ["reading", "hiking", "coding"]
});
// Find the user and show hobbies
const user = db.users.findOne({ name: "Alice" });
printjson(user);Important Notes
Arrays keep related data together, making queries easier.
You can query inside arrays using special operators like $elemMatch.
Remember, arrays can hold mixed types but it's best to keep them consistent for easier use.
Summary
Arrays store multiple values inside one document field.
They can hold strings, numbers, objects, or other arrays.
Use arrays to group related data like lists or sets of items.
Practice
1. What is the main purpose of using
arrays in MongoDB documents?easy
Solution
Step 1: Understand what arrays do in MongoDB
Arrays allow storing multiple values inside one document field, like a list.Step 2: Compare options with array purpose
Only To store multiple values in a single field correctly describes storing multiple values in one field.Final Answer:
To store multiple values in a single field -> Option DQuick 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
Solution
Step 1: Recall MongoDB array syntax
Arrays in MongoDB are defined using square brackets [] with comma-separated values.Step 2: Check each option's syntax
{ tags: ["mongodb", "database"] } uses square brackets correctly. Options A, B, and D use incorrect syntax for arrays.Final Answer:
{ tags: ["mongodb", "database"] } -> Option CQuick 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
Solution
Step 1: Understand MongoDB array matching
Querying with { scores: 90 } matches documents where the array contains the value 90.Step 2: Analyze the given document and query
The scores array includes 90, so the document matches and will be returned.Final Answer:
Documents where the scores array contains 90 -> Option AQuick 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
Solution
Step 1: Understand $push operator usage
$push adds a single value to an array field; it accepts a single value, not necessarily an array.Step 2: Check the query structure
The filter {_id: 1} is present, and $push is used correctly to add "new" to tags array.Final Answer:
The $push operator is used correctly; no error -> Option BQuick 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
Solution
Step 1: Understand matching objects inside arrays
$elemMatch matches array elements that satisfy all conditions inside it.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/.Final Answer:
{ comments: { $elemMatch: { user: "Bob", text: /Nice/ } } } -> Option AQuick 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
