MongoDB聚合运算符:$filter

文章目录

$filter聚合运算符按照指定的条件返回数组的一个子集,返回数组的所有元素都要满足指定的条件,并按照原顺序返回。

语法

js 复制代码
{
    $filter:
      {
         input: <array>,
         as: <string>,
         cond: <expression>,
         limit: <number expression>
      }
}

参数字段说明:

字段 说明
input 可被解析为数组的表达式
as 可选,代表输入数组元素的变量名称,如果不指定,默认的名称为this,变量可以在条件中引用
cond 逻辑表达式,用来判断数组元素是否符合条件,可以引用as指定的名称来访问数组的每个元素
limit 可选,数值表达式用来限制返回元素的数量,其值必须必须大于等于1,匹配的数组元素按照原有的顺序返回。如果limit的值大于匹配的数组元素数量或为null,将返回全部符合条件的元素

使用

举例说明几种情况

不指定limit

js 复制代码
{
  $filter: {
     input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
     as: "num",
     cond: { $isNumber: "$$num" }
  }
}

结果:

js 复制代码
[ 1, 2, 3.1, NumberLong(4) ]

指定limit为常量

js 复制代码
{
  $filter: {
     input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
     as: "num",
     cond: { $isNumber: "$$num" },
     limit: 2
  }
}

结果:

js 复制代码
[ 1, 2 ]

指定limit为表达式

js 复制代码
{
  $filter: {
     input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
     as: "num",
     cond: { $isNumber: "$$num" },
     limit: { $add: [ 0, 1 ] }
  }
}

结果:

js 复制代码
[1]

举例

使用下面的脚本创建sales集合:

js 复制代码
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的条目:

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

执行结果:

js 复制代码
[
   {
      _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: [] }
]

使用limit

本例使用limit字段,限制返回元素数量

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

执行结果:

js 复制代码
[
   {
      _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: [] }
]

limit的数量大于筛选后的元素数量

本例中limit指定的数量大于符合条件的数组元素数量:

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

结果如下:

js 复制代码
[
   {
      _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: [] }
]

字符串相等的匹配条件

下面的聚合中,筛选出items元素名称namepen的元素

js 复制代码
db.sales.aggregate( [
   {
      $project: {
         items: {
            $filter: {
               input: "$items",
               as: "item",
               cond: { $eq: [ "$$item.name", "pen"] }
            }
         }
      }
   }
] )

结果如下:

js 复制代码
[
   {
      _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: [] }
]

使用正则表达式筛选

下面的聚合使用$regexMatch运算符筛选itemsname以字母p开头的元素:

js 复制代码
db.sales.aggregate( [
   {
      $project: {
         items: {
            $filter: {
               input: "$items",
               as: "item",
               cond: {
                  $regexMatch: { input: "$$item.name", regex: /^p/ }
               }
            }
         }
      }
   }
] )

执行结果:

js 复制代码
[
   {
      _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' } ]
   }
]
相关推荐
马克Markorg5 小时前
常见的向量数据库和具有向量数据库能力的数据库
数据库
Coder_Boy_7 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
helloworldandy7 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
数据知道9 小时前
PostgreSQL 故障排查:如何找出数据库中最耗时的 SQL 语句
数据库·sql·postgresql
qq_12498707539 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
枷锁—sha9 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
Coder_Boy_9 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Gain_chance9 小时前
35-学习笔记尚硅谷数仓搭建-DWS层最近n日汇总表及历史至今汇总表建表语句
数据库·数据仓库·hive·笔记·学习
此生只爱蛋9 小时前
【Redis】主从复制
数据库·redis
马猴烧酒.10 小时前
【面试八股|JAVA多线程】JAVA多线程常考面试题详解
java·服务器·数据库