MongoDB聚合运算符:$getField

文章目录

$getField聚合运算符返回文档中指定字段的值,字段名可以包含点.或以$开头。如果不指定,$getField$$CURRENT作为字段返回。

语法

js 复制代码
{
  $getField: {
    field: <String>,
    input: <Object>
  }
}

字段说明:

  • field,字符串类型,指定数据对象中要返回的字段,可以是字符串表达式,如果字段以$符号开头,则需要把字段名放在$literal表达式返回。
  • input,对象类型,缺省值为$$CURRENT,可以是一个包含了指定字段值的表达式,input必须要能解析为一个对象、缺失、空或未定义,如果省略,默认为当前管道中处理的文档$$CURRENT

$$CURRENT返回字段值的简单写法如下:

js 复制代码
{
  $getField: <String>
}

使用

  • 如果field不是字符串类型,$getField返回错误
  • 如果input对象中不存在field或者未指定input$$CURRENT不不存在field字段,$getField返回missing
  • 如果input计算结果为missingundefinednull$getField返回null
  • 如果input计算结果不是对象、missingundefinednull,则$getField返回错误
  • $getField不会隐式遍历对象或数组,例如:$getField执行时会将fielda.b.c认为是a.b.c而不是嵌套字段{a:{b:{c}}}

举例

包含点号(.)的查询

inventory集合的内容如下:

json 复制代码
{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }
{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }
{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }
{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }
{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }

下面的操作使用$getField$gt运算符查找价格price.usd超过200的产品:

js 复制代码
db.inventory.aggregate( [
  {
    $match:
      { $expr:
        { $gt: [ { $getField: "price.usd" }, 200 ] }
      }
   }
] )

操作返回下面的结果:

js 复制代码
[
  { _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
  { _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 }
]

查询以($)开头的字段

集合inventory有下面的文档:

json 复制代码
{ "_id" : 1, "item" : "sweatshirt", "$price": 45.99, qty: 300 }
{ "_id" : 2, "item" : "winter coat", "$price": 499.99, qty: 200 }
{ "_id" : 3, "item" : "sun dress", "$price": 199.99, qty: 250 }
{ "_id" : 4, "item" : "leather boots", "$price": 249.99, qty: 300 }
{ "_id" : 5, "item" : "bow tie", "$price": 9.99, qty: 180 }

下面的操作使用$getField$gt$literal运算符查找价格price大于200的产品:

js 复制代码
db.inventory.aggregate( [
  {
    $match:
      { $expr:
        { $gt: [ { $getField: {$literal: "$price" } }, 200 ] }
      }
   }
] )

操作返回下面的结果:

js 复制代码
[
  { _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
  { _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 }
]

子文档字段查询

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

js 复制代码
db.inventory.insertMany( [
   { "_id" : 1, "item" : "sweatshirt",  "price.usd": 45.99,
     "quantity": { "$large": 50, "$medium": 50, "$small": 25 }
   },
   { "_id" : 2, "item" : "winter coat", "price.usd": 499.99,
     "quantity": { "$large": 35, "$medium": 35, "$small": 35 }
   },
   { "_id" : 3, "item" : "sun dress", "price.usd": 199.99,
     "quantity": { "$large": 45, "$medium": 40, "$small": 5 }
   },
   { "_id" : 4, "item" : "leather boots", "price.usd": 249.99,
     "quantity": { "$large": 20, "$medium": 30, "$small": 40 }
   },
   { "_id" : 5, "item" : "bow tie", "price.usd": 9.99,
     "quantity": { "$large": 0, "$medium": 10, "$small": 75 }
   }
] )

下面的操作返$small的值小于等于20的文档:

js 复制代码
db.inventory.aggregate( [
   { $match:
      { $expr:
         { $lte:
            [
               { $getField:
                  { field: { $literal: "$small" },
                    input: "$quantity"
                  }
               },
               20
            ]
         }
      }
   }
] )
  • $lte 运算符用于查找小于等于20的文档
  • $getField需要明确的fieldinput参数,因为$small字段是子文档的一部分
  • 因为字段名前面有$符号,$getField使用$literal来计算"$small"

输出结果如下:

js 复制代码
[
  {
    _id: 3,
    item: 'sun dress',
    'price.usd': 199.99,
    quantity: { '$large': 45, '$medium': 40, '$small': 5 }
  }
]
相关推荐
阿标在干嘛21 小时前
政策平台的推送系统:消息队列、定时任务、AB测试的工程实践
服务器·数据库·ab测试
Upsy-Daisy21 小时前
Hermes Agent 学习笔记 02:安装、配置与第一次运行
java·前端·数据库
Tongpao_SSDHDD1 天前
希捷酷鹰ST6000VX008实测解析:中小安防监控高性价比存储方案
大数据·数据库·人工智能
蓝鸟19741 天前
Oracle超大DMP备份文件瘦身、日志精简、磁盘空间优化实战方案日志
数据库·oracle·数据库运维·生产运维实战·oracle避坑·磁盘空间优化·oracle日志清理
金融支付架构实战指南1 天前
CQRS + 命令模式 + 事件驱动 + 数据库持久化
数据库·ddd·命令模式·领域驱动模型
sevenll071 天前
DocKit agentic MongoDB GUI 客户端 - 用自然语言和你的数据对话
数据库·mongodb·nosql·agent·桌面客户端
团象科技1 天前
从一线实操案例拆解不同出海团队落地海外VPS运维独立站的路径细节
大数据·数据库·人工智能
小马爱打代码1 天前
框架 - 组件 - 中间件:生产级参数配置指引
数据库·中间件
asdfg12589631 天前
一文通俗理解JDBC中的核心概念+案例
java·数据库·oracle·jdbc
点灯小铭1 天前
基于单片机与DAC0832的双路波形信号发生系统设计
数据库·单片机·mongodb·毕业设计·课程设计·期末大作业