0
0
MongodbHow-ToBeginner · 2 min read

MongoDB Query to Find Documents Where Array Contains Value

Use the query { arrayField: value } in MongoDB to find documents where the array arrayField contains the specified value.
📋

Examples

Input{ tags: "mongodb" }
Output[{ _id: 1, tags: ["mongodb", "database"] }, { _id: 3, tags: ["mongodb"] }]
Input{ scores: 85 }
Output[{ _id: 2, scores: [70, 85, 90] }]
Input{ colors: "red" }
Output[]
🧠

How to Think About It

To find documents where an array contains a value, think of the array like a list of items. You want to check if your target value is one of those items. MongoDB lets you do this by simply querying the array field with the value you want, using { arrayField: value }.
📐

Algorithm

1
Get the collection to search.
2
Specify the array field and the value to find inside it.
3
Use a query that matches documents where the array contains that value.
4
Return all matching documents.
💻

Code

mongodb
db.products.insertMany([
  { _id: 1, tags: ["mongodb", "database"] },
  { _id: 2, tags: ["nosql", "database"] },
  { _id: 3, tags: ["mongodb"] }
]);

const query = { tags: "mongodb" };
const result = db.products.find(query).toArray();
printjson(result);
Output
[ { "_id" : 1, "tags" : [ "mongodb", "database" ] }, { "_id" : 3, "tags" : [ "mongodb" ] } ]
🔍

Dry Run

Let's trace the query { tags: "mongodb" } through the code

1

Insert documents

Documents inserted with tags arrays: ["mongodb", "database"], ["nosql", "database"], ["mongodb"]

2

Run query

Find documents where tags array contains "mongodb"

3

Return results

Documents with _id 1 and 3 match and are returned

_idtagsContains 'mongodb'?
1["mongodb", "database"]Yes
2["nosql", "database"]No
3["mongodb"]Yes
💡

Why This Works

Step 1: Simple matching

MongoDB matches array fields by checking if the array contains the value directly using { arrayField: value }.

Step 2: No special operator needed

You do not need to use special operators like $elemMatch for simple value containment.

Step 3: Efficient query

This query is efficient because MongoDB can use indexes on array fields to speed up the search.

🔄

Alternative Approaches

Using $elemMatch operator
mongodb
db.products.find({ tags: { $elemMatch: { $eq: "mongodb" } } })
More verbose but useful when matching multiple conditions inside array elements.
Using $in operator
mongodb
db.products.find({ tags: { $in: ["mongodb"] } })
Useful when checking if array contains any value from a list of values.

Complexity: O(n) time, O(k) space

Time Complexity

The query scans documents and checks array fields, which is O(n) where n is number of documents. Indexes on array fields can improve this.

Space Complexity

The query uses O(k) space where k is number of matching documents returned.

Which Approach is Fastest?

Direct matching { arrayField: value } is fastest for simple containment. Alternatives add overhead for complex conditions.

ApproachTimeSpaceBest For
Direct match { arrayField: value }O(n)O(k)Simple value containment
$elemMatch operatorO(n)O(k)Multiple conditions inside array elements
$in operatorO(n)O(k)Matching any value from a list
💡
For simple checks if an array contains a value, just query with { arrayField: value } without extra operators.
⚠️
Beginners often try to use $elemMatch unnecessarily for simple value checks inside arrays.