How to Query Array Fields in MongoDB: Syntax and Examples
To query arrays in MongoDB, use
find() with operators like $elemMatch to match elements, $in to check for values in arrays, or dot notation to query nested fields inside arrays. These let you filter documents based on array contents efficiently.Syntax
MongoDB provides several ways to query arrays:
{ arrayField: value }: Matches documents wherearrayFieldcontainsvalue.{ arrayField: { $elemMatch: { criteria } } }: Matches documents where at least one element inarrayFieldmeetscriteria.{ 'arrayField.field': value }: Uses dot notation to query fields inside objects within arrays.{ arrayField: { $in: [values] } }: Matches documents wherearrayFieldcontains any value in the list.
mongodb
db.collection.find({ arrayField: value })
db.collection.find({ arrayField: { $elemMatch: { key: value } } })
db.collection.find({ 'arrayField.key': value })
db.collection.find({ arrayField: { $in: [value1, value2] } })Example
This example shows how to find documents where the tags array contains the value "mongodb", and how to find documents where an array of objects grades has an element with score greater than 80.
mongodb
db.students.insertMany([
{ name: "Alice", tags: ["mongodb", "database", "nosql"], grades: [{score: 85, subject: "math"}, {score: 78, subject: "english"}] },
{ name: "Bob", tags: ["sql", "database"], grades: [{score: 90, subject: "math"}, {score: 88, subject: "english"}] },
{ name: "Charlie", tags: ["nosql", "mongodb"], grades: [{score: 70, subject: "math"}, {score: 60, subject: "english"}] }
])
// Find students with tag "mongodb"
db.students.find({ tags: "mongodb" })
// Find students with any grade score > 80
db.students.find({ grades: { $elemMatch: { score: { $gt: 80 } } } })Output
[
{ "_id": ObjectId("..."), "name": "Alice", "tags": ["mongodb", "database", "nosql"], "grades": [{"score": 85, "subject": "math"}, {"score": 78, "subject": "english"}] },
{ "_id": ObjectId("..."), "name": "Charlie", "tags": ["nosql", "mongodb"], "grades": [{"score": 70, "subject": "math"}, {"score": 60, "subject": "english"}] }
]
[
{ "_id": ObjectId("..."), "name": "Alice", "tags": ["mongodb", "database", "nosql"], "grades": [{"score": 85, "subject": "math"}, {"score": 78, "subject": "english"}] },
{ "_id": ObjectId("..."), "name": "Bob", "tags": ["sql", "database"], "grades": [{"score": 90, "subject": "math"}, {"score": 88, "subject": "english"}] }
]
Common Pitfalls
Common mistakes when querying arrays include:
- Using
{ arrayField: { key: value } }instead of$elemMatchfor matching objects inside arrays, which won't work as expected. - Confusing
$inwith$elemMatch:$inchecks if any array element matches any value in a list, but does not match multiple conditions on the same element. - Not using dot notation when querying nested fields inside array objects.
mongodb
/* Wrong: This does NOT find documents where grades array has an element with score 85 and subject 'math' */ db.students.find({ grades: { score: 85, subject: "math" } }) /* Right: Use $elemMatch to match both conditions on the same element */ db.students.find({ grades: { $elemMatch: { score: 85, subject: "math" } } })
Quick Reference
| Operator | Description | Example |
|---|---|---|
| { arrayField: value } | Matches documents where array contains value | { tags: "mongodb" } |
| $elemMatch | Matches documents where an array element meets multiple criteria | { grades: { $elemMatch: { score: { $gt: 80 } } } } |
| Dot notation | Query nested fields inside array objects | { 'grades.subject': "math" } |
| $in | Matches if array contains any value in list | { tags: { $in: ["sql", "nosql"] } } |
Key Takeaways
Use simple equality to find if an array contains a value.
Use $elemMatch to match multiple conditions on the same array element.
Use dot notation to query fields inside objects within arrays.
Avoid confusing $in with $elemMatch; they serve different purposes.
Always test queries to ensure they match the intended array elements.