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' } ]
   }
]
相关推荐
Cikiss15 分钟前
微服务实战——平台属性
java·数据库·后端·微服务
小小不董29 分钟前
《Linux从小白到高手》理论篇:深入理解Linux的网络管理
linux·运维·服务器·数据库·php·dba
无敌少年小旋风1 小时前
MySQL 内部优化特性:索引下推
数据库·mysql
柒小毓1 小时前
将excel导入SQL数据库
数据库
bug菌¹1 小时前
滚雪球学Oracle[2.5讲]:数据库初始化配置
数据库·oracle·数据库初始化·初始化配置
一休哥助手1 小时前
Redis 五种数据类型及底层数据结构详解
数据结构·数据库·redis
翔云1234561 小时前
MVCC(多版本并发控制)
数据库·mysql
代码敲上天.2 小时前
数据库语句优化
android·数据库·adb
盒马盒马2 小时前
Redis:zset类型
数据库·redis
静听山水2 小时前
mysql语句执行过程
数据库·mysql