MongoDB- Find a document with an array containing a specific value

Today in this article, we will see how to query MongoDB- Find document with array contains a specific value.
Updated October 2026: a section on $in vs $all vs $elemMatch, a range and aggregation section with index notes, and an FAQ.
We will cover below a few specific scenarios like dealing with [] array type elements,
- Get documents based on matching array size i.e where array size is greater or less than of given specified size.
- Find the array that contains specific values only.
- Specify conditions for array elements.
Today in this article, we will cover below aspects,
MongoDB – Find document by specified array value
Command
{ <field>: {<value> }
The below example shows an array filter where we get the list of documents where the value of an array element i.e books[] is equal to the value specified.
Example
{ books: "white"}

MongoDB – Find documents by an array length
Command
{ <array-field>: {$size :<size-specified> }
The below example shows an array filter where we get the list of documents based on array size i.e domain [] size is equal to the size specified.
Example
{ domain: { $size: 1} }

MongoDB – Find a document with an array by specifying multiple conditions
Command
{ <field>: {$gt :<size-specified> }
The below example shows an array filter where we get the list of documents where the value of any array element i.e domain [] is greater than 14.
{ domain: { $gt: 14 } }
The above query returns all the documents where an array field “domain” contains value 14.

References: MongoDB Naming Standards and Guidelines
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published (2026) best practices and guidelines for software design and development.
Frequently asked questions
How do I find documents where an array contains a specific value in MongoDB?
Use plain equality on the array field, for example find({ tags: "red" }). It matches any document where at least one element equals the value. The same filter shape works in mongosh and in drivers such as Node.js, where you’d write db.collection('inventory').find({ tags: 'red' }).
Don’t pass a whole array like { tags: ["red", "blank"] } unless you want an exact match. That form requires the same elements in the same order as the stored array.
What is the difference between $in and $all in MongoDB?
$in matches if the array has at least one of the listed values. $all matches only if the array contains every listed value, in any order, and extra elements are allowed. $all with an empty array matches nothing, which surprises many people.
As a rule of thumb, { tags: { $all: ["a", "b"] } } behaves like an $and of two plain equality conditions, one for each value.
When do I need $elemMatch instead of plain equality or dot notation?
Use $elemMatch when several conditions must hold on the same array element, such as a range on a scalar array or two fields of an embedded document. Without it, different elements can satisfy different conditions.
For example, { dim_cm: { $gt: 15, $lt: 20 } } can match when one element is above 15 and another is below 20. Wrapping the same conditions in $elemMatch forces one element to meet both. The same applies to 'lineItems.productId' and 'lineItems.qty' written as separate dot-notation conditions.
Does MongoDB use an index when querying an array field?
Yes. A multikey index like createIndex({ tags: 1 }) supports equality and $in queries on the array. Confirm it with explain() and check that the winning plan uses an index scan rather than a collection scan.
The aggregation form { $match: { $expr: { $in: ["mongodb", "$tags"] } } } doesn’t use the index the way the query operator does. Prefer the plain query filter in $match, and keep that stage early in the pipeline.