⬅ Previous Topic
MongoDB Query Operators OverviewNext Topic ⮕
Querying Nested Fields in MongoDB⬅ Previous Topic
MongoDB Query Operators OverviewNext Topic ⮕
Querying Nested Fields in MongoDBMongoDB 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.
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).
An embedded document is a nested JSON object stored within a field of a MongoDB document. This helps logically group related data together.
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"
}
});
{ acknowledged: true, insertedId: ObjectId("...") }
Explanation: The subjects
field is an array, while contact
is an embedded object with two fields: phone and email.
You can easily query whether an array contains a specific value.
db.students.find({ subjects: "Math" });
{ _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.
$all
to Match Multiple Array ValuesIf you want to ensure that an array contains multiple specific elements, use $all
.
db.students.find({ subjects: { $all: ["Math", "English"] } });
Matching documents where both "Math" and "English" are present in the subjects array.
Q: Will { subjects: "Math" }
match if the array is ["Mathematics", "Science"]
?
A: No. MongoDB performs exact value matching. "Math" ≠ "Mathematics".
You can also query a specific position in an array using dot notation.
db.students.find({ "subjects.0": "Math" });
Matches students whose first subject in the array is "Math".
You can match embedded documents by providing the exact structure:
db.students.find({
contact: { phone: "1234567890", email: "john@example.com" }
});
Returns document only if both fields match exactly in the embedded object.
To match just one field inside an embedded document:
db.students.find({ "contact.phone": "1234567890" });
{ _id: ObjectId("..."), name: "John Doe", ... }
Explanation: Dot notation helps access nested fields like contact.phone
.
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 }
]
});
db.teachers.find({ "classes.subject": "Physics" });
Returns documents where any item in the 'classes' array has subject = "Physics".
db.teachers.find({
classes: { subject: "Math", grade: 10 }
});
Note: This only matches when the object matches exactly (including order and fields).
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.
In the next topic, we’ll learn about Pagination using limit(), skip(), and sort() to manage large query results efficiently.
⬅ Previous Topic
MongoDB Query Operators OverviewNext Topic ⮕
Querying Nested Fields in MongoDBYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.