$filter(聚合)
定义
兼容性
您可以在以下环境中使用$filter
MongoDB Atlas:云中MongoDB部署的全面托管服务
MongoDB Enterprise:基于订阅的、自行管理的MongoDB版本
MongoDB Community:源代码可用的、免费使用并自行管理的MongoDB版本
语法
$filter
的语法如下
{ $filter: { input: <array>, as: <string>, cond: <expression>, limit: <number expression> } }
字段 | 指定 |
---|---|
输入 | |
作为 | |
条件 | |
限制 |
有关表达式更多信息,请参阅 表达式运算符。
行为
示例 | 结果 | ||||||||
---|---|---|---|---|---|---|---|---|---|
| [ 1, 2, 3.1, NumberLong(4) ] | ||||||||
| [ 1, 2 ] | ||||||||
| [ 1 ] |
示例
集合 sales
包含以下文档
db.sales.insertMany( [ { _id: 0, items: [ { item_id: 43, quantity: 2, price: 10, name: "pen" }, { item_id: 2, quantity: 1, price: 240, name: "briefcase" } ] }, { _id: 1, items: [ { item_id: 23, quantity: 3, price: 110, name: "notebook" }, { item_id: 103, quantity: 4, price: 5, name: "pen" }, { item_id: 38, quantity: 1, price: 300, name: "printer" } ] }, { _id: 2, items: [ { item_id: 4, quantity: 1, price: 23, name: "paper" } ] } ] )
基于数字比较进行筛选
以下示例筛选 items
数组,只包括具有 price
大于或等于 100
的文档
db.sales.aggregate( [ { $project: { items: { $filter: { input: "$items", as: "item", cond: { $gte: [ "$$item.price", 100 ] } } } } } ] )
[ { _id: 0, items: [ { item_id: 2, quantity: 1, price: 240, name: 'briefcase' } ] }, { _id: 1, items: [ { item_id: 23, quantity: 3, price: 110, name: 'notebook' }, { item_id: 38, quantity: 1, price: 300, name: 'printer' } ] }, { _id: 2, items: [] } ]
使用限制字段
此示例使用前一个示例中的 sales
集合。
示例使用 limit
字段来指定每个 items
数组中返回的匹配元素的数量。
db.sales.aggregate( [ { $project: { items: { $filter: { input: "$items", as: "item", cond: { $gte: [ "$$item.price", 100 ] }, limit: 1 } } } } ] )
[ { _id: 0, items: [ { item_id: 2, quantity: 1, price: 240, name: 'briefcase' } ] }, { _id: 1, items: [ { item_id: 23, quantity: 3, price: 110, name: 'notebook' } ] }, { _id: 2, items: [] } ]
限制大于可能匹配数
此示例使用前一个示例中的 sales
集合。
该示例使用了一个值大于可能返回的匹配元素数量的 limit
字段。在这种情况下,limit
不影响查询结果,并返回所有符合 $gte
过滤条件的文档。
db.sales.aggregate( [ { $project: { items: { $filter: { input: "$items", as: "item", cond: { $gte: [ "$$item.price", 100] }, limit: 5 } } } } ] )
[ { _id: 0, items: [ { item_id: 2, quantity: 1, price: 240, name: 'briefcase' } ] }, { _id: 1, items: [ { item_id: 23, quantity: 3, price: 110, name: 'notebook' }, { item_id: 38, quantity: 1, price: 300, name: 'printer' } ] }, { _id: 2, items: [] } ]
基于字符串相等匹配的过滤
此示例使用前一个示例中的 sales
集合。
以下聚合过滤用于匹配具有 name
值为 pen
的 items
。
db.sales.aggregate( [ { $project: { items: { $filter: { input: "$items", as: "item", cond: { $eq: [ "$$item.name", "pen"] } } } } } ] )
[ { _id: 0, items: [ { item_id: 43, quantity: 2, price: 10, name: 'pen' } ] }, { _id: 1, items: [ { item_id: 103, quantity: 4, price: 5, name: 'pen' } ] }, { _id: 2, items: [] } ]
基于正则表达式匹配的过滤
此示例使用前一个示例中的 sales
集合。
以下聚合使用 $regexMatch
来筛选具有以 p
开头的 name
值的 items
。
db.sales.aggregate( [ { $project: { items: { $filter: { input: "$items", as: "item", cond: { $regexMatch: { input: "$$item.name", regex: /^p/ } } } } } } ] )
[ { _id: 0, items: [ { item_id: 43, quantity: 2, price: 10, name: 'pen' } ] }, { _id: 1, items: [ { item_id: 103, quantity: 4, price: 5, name: 'pen' }, { item_id: 38, quantity: 1, price: 300, name: 'printer' } ] }, { _id: 2, items: [ { item_id: 4, quantity: 1, price: 23, name: 'paper' } ] } ]