On this page本页内容
$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>
}
}
input | |
cond | input array individually with the variable name specified in as.as中指定的变量名分别引用input数组的每个元素。
|
as | input array. this.this。 |
limit |
|
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
{
$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: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" : [ ] }
limit fieldlimit字段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" : [ ] }
limitThis 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 }
]
}
limitThis 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": []
}
]