Docs HomeMongoDB Manual

$filter (aggregation)

Definition定义

$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: <array>,
cond: <expression>,
as: <string>,
limit: <number expression>
}
}
Field字段Specification规格
inputAn expression that resolves to an array.解析为数组的表达式
condAn 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中指定的变量名分别引用input数组的每个元素。
asOptional.可选的。A name for the variable that represents each individual element of the input array. 变量的名称,表示input数组的每个单独元素。If no name is specified, the variable name defaults to this.如果未指定名称,则变量名称默认为this
limitOptional.可选的。A number expression that restricts the number of matching array elements that $filter returns. 一个数字表达式,用于限制$filter返回的匹配数组元素的数量。You cannot specify a limit less than 1. The matching array elements are returned in the order they appear in the input array.您不能指定小于1的限制。匹配的数组元素按它们在输入数组中出现的顺序返回。
If the specified limit is greater than the number of matching array elements, $filter returns all matching array elements. 如果指定的limit大于匹配数组元素的数量,$filter将返回所有匹配的数组元素。If the limit is null, $filter returns all matching array elements. 如果限制为null$filter将返回所有匹配的数组元素。

For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

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") ] },
{ $lte: [ "$$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") ] },
{ $lte: [ "$$num", NumberLong("9223372036854775807") ] }
] },
limit: { $add: [ 0, 1 ]}
}
}
[ 1 ]

Examples实例

A collection sales has the following documents:集合sales具有以下文档:

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:以下示例筛选items数组,使其仅包括price大于或等于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" : [ ] }

Using the limit field使用limit字段

This example uses the sales collection from the previous example.本示例使用上一示例中的sales集合。

The example uses the limit field to specifiy the number of matching elements returned in each items array.该示例使用limit字段指定在每个items数组中返回的匹配元素的数量。

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" : [ ] }

limit as a Numeric Expression作为数字表达式

This example uses the sales collection from the previous example.本示例使用上一示例中的sales集合。

The following example uses a numeric expression for the limit field to specifiy the number of matching elements returned in each items array.以下示例使用limit字段的数字表达式来指定在每个items数组中返回的匹配元素的数量。

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 }
]
}

limit Greater than Possible Matches大于可能匹配

This example uses the sales collection from the previous example.本示例使用上一示例中的sales集合。

The example uses a limit field value that is larger than the possible number of matching elements that can be returned.该示例使用的limit字段值大于可以返回的匹配元素的可能数量。

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": []
}
]