Working with Arrays and Embedded Documents in MongoDB
MongoDB allows storing arrays and embedded documents directly inside documents. This makes data modeling more intuitive and eliminates the need for complex joins, which are often required in SQL databases.
What is an Array in MongoDB?
An array is a list of values stored in a single field. These values can be of any type — strings, numbers, or even objects (embedded documents).
What is an Embedded Document?
An embedded document is a nested JSON object stored within a field of a MongoDB document. This helps logically group related data together.
Insert Example: Array and Embedded Document
Let’s insert a student document with subjects (array of strings) and contact (embedded document).
db.students.insertOne({
name: "John Doe",
age: 18,
subjects: ["Math", "Science", "English"],
contact: {
phone: "1234567890",
email: "john@example.com"
}
});
Output:
{ acknowledged: true, insertedId: ObjectId("...") }
Explanation: The subjects
field is an array, while contact
is an embedded object with two fields: phone and email.
Querying Arrays
You can easily query whether an array contains a specific value.
db.students.find({ subjects: "Math" });
Output:
{ _id: ObjectId("..."), name: "John Doe", age: 18, subjects: ["Math", "Science", "English"], contact: { phone: "1234567890", email: "john@example.com" } }
Explanation: This matches any document where the subjects
array contains "Math". No need for special operators — simple match works.
Using $all
to Match Multiple Array Values
If you want to ensure that an array contains multiple specific elements, use $all
.
db.students.find({ subjects: { $all: ["Math", "English"] } });
Output:
Matching documents where both "Math" and "English" are present in the subjects array.
Intuition Check
Q: Will { subjects: "Math" }
match if the array is ["Mathematics", "Science"]
?
A: No. MongoDB performs exact value matching. "Math" ≠ "Mathematics".
Querying Array by Index
You can also query a specific position in an array using dot notation.
db.students.find({ "subjects.0": "Math" });
Output:
Matches students whose first subject in the array is "Math".
Querying Embedded Documents
You can match embedded documents by providing the exact structure:
db.students.find({
contact: { phone: "1234567890", email: "john@example.com" }
});
Output:
Returns document only if both fields match exactly in the embedded object.
Using Dot Notation in Embedded Document Queries
To match just one field inside an embedded document:
db.students.find({ "contact.phone": "1234567890" });
Output:
{ _id: ObjectId("..."), name: "John Doe", ... }
Explanation: Dot notation helps access nested fields like contact.phone
.
Querying Arrays of Embedded Documents
Let’s insert a document with an array of embedded documents:
db.teachers.insertOne({
name: "Ms. Smith",
classes: [
{ subject: "Math", grade: 10 },
{ subject: "Physics", grade: 12 }
]
});
Matching a Field in Array of Embedded Documents
db.teachers.find({ "classes.subject": "Physics" });
Output:
Returns documents where any item in the 'classes' array has subject = "Physics".
Matching Entire Embedded Object in an Array
db.teachers.find({
classes: { subject: "Math", grade: 10 }
});
Note: This only matches when the object matches exactly (including order and fields).
Intuition Check
Q: Can I query partial values of an embedded document using the full object?
A: No. MongoDB requires exact match for embedded object unless you use dot notation.
Summary
- Arrays allow storing multiple values in a single field
- Embedded documents store structured/nested data inside documents
- Use dot notation to query nested fields effectively
- MongoDB's native support for complex structures makes it ideal for real-world data modeling
In the next topic, we’ll learn about Pagination using limit(), skip(), and sort() to manage large query results efficiently.