MongoDB
database
array
query
element

MongoDB How to find out if an array field contains an element?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In MongoDB, checking whether an array contains a value is often simpler than it looks. For arrays of scalar values, a direct equality match is usually enough, while operators such as $in, $all, and $elemMatch are useful when the match becomes more specific.

The important distinction is whether the array contains simple values or embedded documents. That determines whether a direct match is enough or whether you need an operator that ties multiple conditions to the same array element.

Direct Match for Scalar Arrays

If the array contains plain strings, numbers, or ObjectIds, MongoDB can match membership directly.

Given:

javascript
1{
2  name: "Product 1",
3  tags: ["electronics", "sale", "new"]
4}

You can query for membership like this:

javascript
db.products.find({ tags: "sale" })

This means "find documents where the tags array contains the value sale."

Match Any of Several Values with $in

If you want documents where the array contains any one of several candidates, use $in:

javascript
db.products.find({
  tags: { $in: ["sale", "discount"] }
})

This matches if at least one listed value appears in the array.

Require Multiple Values with $all

If all listed values must be present, use $all:

javascript
db.products.find({
  tags: { $all: ["electronics", "sale"] }
})

This is different from $in. $all requires every listed value, while $in accepts any single match.

Arrays of Embedded Documents

If the array stores objects, use $elemMatch when multiple conditions must apply to the same element.

Example document:

javascript
1{
2  name: "Order 100",
3  items: [
4    { sku: "A1", quantity: 2 },
5    { sku: "B2", quantity: 5 }
6  ]
7}

To find orders where one item has sku equal to B2 and quantity greater than 3:

javascript
1db.orders.find({
2  items: {
3    $elemMatch: {
4      sku: "B2",
5      quantity: { $gt: 3 }
6    }
7  }
8})

Without $elemMatch, separate conditions can accidentally match different elements of the array.

Checking for Field Presence

Sometimes the real issue is that the array field may not exist. You can combine membership logic with $exists if the schema is flexible:

javascript
db.products.find({
  tags: { $exists: true, $in: ["sale"] }
})

That is not always necessary, but it can make intent clearer when documents vary in shape.

Another subtle case is null. A missing field, a null field, and an empty array are not identical in MongoDB queries, so be clear about which case your application actually intends to match.

Performance and Indexing

Array membership queries can use indexes. If this pattern is common, create an index on the array field:

javascript
db.products.createIndex({ tags: 1 })

MongoDB creates a multikey index, which helps membership lookups scale better on larger collections.

That does not remove the need to design the query well, but it prevents basic membership checks from turning into full collection scans as the dataset grows.

Common Pitfalls

  • Using $elemMatch for a simple scalar membership test when a direct match would be clearer.
  • Confusing $in and $all; they answer different questions.
  • Forgetting that separate conditions on arrays of embedded documents may match different elements unless you use $elemMatch.
  • Ignoring indexing on array fields that are queried frequently.
  • Adding $exists everywhere even when the schema already guarantees the field.

Summary

  • For scalar arrays, { field: value } is often enough to test containment.
  • Use $in when any one of several values is acceptable.
  • Use $all when all listed values must be present.
  • Use $elemMatch for arrays of embedded documents when multiple predicates must hit the same element.
  • Add a multikey index for array fields that are queried often.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.