How do i filter an array in mongodb?

Docs HomeMongoDB Manual

On this page

  • Match an Array
  • Query an Array for an Element
  • Specify Multiple Conditions for Array Elements
  • Additional Query Tutorials


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.


The following example queries for all documents where the field tags value is an array with exactly two elements, "red" and "blank", in the specified order:

If, instead, you wish to find an array that contains both the elements "red" and "blank", without regard to order or other elements in the array, use the $all operator:

The following example queries for all documents where tags is an array that contains the string "red" as one of its elements:

For example, the following operation queries for all documents where the array dim_cm contains at least one element whose value is greater than 25.

When specifying compound conditions on array elements, you can specify the query such that either a single array element meets these condition or any combination of array elements meets the conditions.

The following example queries for documents where the dim_cm array contains elements that in some combination satisfy the query conditions; e.g., one element can satisfy the greater than 15 condition and another element can satisfy the less than 20 condition, or a single element can satisfy both:

Use $elemMatch operator to specify multiple criteria on the elements of an array such that at least one array element satisfies all the specified criteria.

The following example queries for documents where the dim_cm array contains at least one element that is both greater than ($gt) 22 and less than ($lt) 30:

Using dot notation, you can specify query conditions for an element at a particular index or position of the array. The array uses zero-based indexing.

Note

When querying using dot notation, the field and nested field must be inside quotation marks.

The following example queries for all documents where the second element in the array dim_cm is greater than 25:

Use the $size operator to query for arrays by number of elements. For example, the following selects documents where the array tags has 3 elements.

For additional query examples, see:

  • Query Documents

  • Query on Embedded/Nested Documents

  • Query an Array of Embedded Documents

Docs HomeMongoDB Manual

$filter

Selects a subset of an array to return based on the specified condition. Returns an array with only those elements that match the condition. The returned elements are in the original order.

$filter has the following syntax:

{
$filter:
{
input: ,
cond: ,
as: ,
limit: <number expression>
}
}

Field

Specification

input

An expression that resolves to an array.

cond

An expression that resolves to a boolean value used to determine if an element should be included in the output array. The expression references each element of the input array individually with the variable name specified in as.

as

Optional. A name for the variable that represents each individual element of the input array. If no name is specified, the variable name defaults to this.

limit

Optional. A number expression that restricts the number of matching array elements that $filter returns. You cannot specify a limit less than 1. The matching array elements are returned in the order they appear in the input array.

If the specified limit is greater than the number of matching array elements, $filter returns all matching array elements. If the limit is null, $filter returns all matching array elements.

For more information on expressions, see Expressions.

Example

Results

{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and: [
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
}
}

[ 1, 2, 3.1, NumberLong(4) ]

{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and:[
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $gte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
limit: 2
}
}

[ 1, 2 ]

{
$filter: {
input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
as: "num",
cond: { $and:[
{ $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
{ $gte: [ "$$num", NumberLong("9223372036854775807") ] }
] }
limit: { $add: [ 0, 1 ]}
}
}

[ 1 ]

A collection sales has the following documents:

db.sales.insertMany( [
{
_id: 0,
items: [
{ item_id: 43, quantity: 2, price: 10 },
{ item_id: 2, quantity: 1, price: 240 }
]
},
{
_id: 1,
items: [
{ item_id: 23, quantity: 3, price: 110 },
{ item_id: 103, quantity: 4, price: 5 },
{ item_id: 38, quantity: 1, price: 300 }
]
},
{
_id: 2,
items: [
{ item_id: 4, quantity: 1, price: 23 }
]
}
] )

The following example filters the items array to only include documents that have a price greater than or equal to 100:

db.sales.aggregate( [
{
$project: {
items: {
$filter: {
input: "$items",
as: "item",
cond: { $gte: [ "$$item.price", 100 ] }
}
}
}
}
] )

The operation produces the following results:

{
"_id" : 0,
"items" : [
{ "item_id" : 2, "quantity" : 1, "price" : 240 }
]
}
{
"_id" : 1,
"items" : [
{ "item_id" : 23, "quantity" : 3, "price" : 110 },
{ "item_id" : 38, "quantity" : 1, "price" : 300 }
]
}
{ "_id" : 2, "items" : [ ] }

This example uses the sales collection from the previous example.

The example uses the limit field to specifiy the number of matching elements returned in each items array.

db.sales.aggregate( [
{
$project: {
items: {
$filter: {
input: "$items",
cond: { $gte: [ "$$item.price", 100 ] },
as: "item",
limit: 1
}
}
}
}
] )

The operation produces the following results:

{
"_id" : 0,
"items" : [
{ "item_id" : 2, "quantity" : 1, "price" : 240 }
]
}
{
"_id" : 1,
"items" : [
{ "item_id" : 23, "quantity" : 3, "price" : 110 }
]
}
{ "_id" : 2, "items" : [ ] }

This example uses the sales collection from the previous example.

The following example uses a numeric expression for the limit field to specifiy the number of matching elements returned in each items array.

db.sales.aggregate( [
{
$project: {
items: {
$filter: {
input: "$items",
cond: { $lte: [ "$$item.price", 150] },
as: "item",
limit: 2.000
}
}
}
}
] )

The operation produces the following results:

{
"_id": 0,
"items": [
{ "item_id": 43, "quantity": 2, "price": 10 }
]
},
{
"_id": 1,
"items": [
{ "item_id": 23, "quantity": 3, "price": 110 },
{ "item_id": 103, "quantity": 4, "price": 5 }
]
},
{
"_id": 2,
"items": [
{ "item_id": 4, "quantity": 1, "price": 23 }
]
}

This example uses the sales collection from the previous example.

The example uses a limit field value that is larger than the possible number of matching elements that can be returned.

db.sales.aggregate( [
{
$project: {
items: {
$filter: {
input: "$items",
cond: { $gte: [ "$$item.price", 100] },
as: "item",
limit: 5
}
}
}
}
] )

The operation produces the following results:

[
{
"_id": 0,
"items": [
{ "item_id": 2, "quantity": 1, "price": 240 }
]
},
{
"_id": 1,
"items": [
{ "item_id": 23, "quantity": 3, "price": 110 },
{ "item_id": 38, "quantity": 1, "price": 300 }
]
},
{
"_id": 2,
"items": []
}
]

How do I filter an array data in MongoDB?

Filter MongoDB Array Element Using $Filter Operator This operator uses three variables: input – This represents the array that we want to extract. cond – This represents the set of conditions that must be met. as – This optional field contains a name for the variable that represent each element of the input array.

How do I query an array of objects in MongoDB?

To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object.

How do I filter data in MongoDB collection?

Parameter description syntax of filter operator in MongoDB..
Filter – The filter operator is used to return the result using specified conditions. ... .
Input – This is an expression that was used to resolves in an array. ... .
As – It is an optional parameter used in the filter operator..

How do you query an array?

To query if the array field contains at least one element with the specified value, use the filter { : } where is the element value. To specify conditions on the elements in the array field, use query operators in the query filter document: { : { : , ... } }