【问题标题】:How to change the output of aggregate values in MongeDB如何更改 MongoDB 中聚合值的输出
【发布时间】:2020-04-19 17:43:37
【问题描述】:

我正在尝试编写一个 Mongodb 查询,如果收入 >70000,则返回名称和收入为 highEarner true,否则返回 false

我使用的文件包括姓名(人名)、职位(法律、人力资源、公关等)和收入(数字)。

我追求的输出是

{"name": "John Doe", HighEarner: true}, 
{"name": "John Loe", HighEarner: false} 

这就是我已经走了多远。

db.collection.aggregrate([
    {
      $group:{
        _id:"income", 
        "Honours":{true:{$income:$gt 70000}, {false;{$income:%lt 70000`}}
    }
}])

但它不起作用任何指针都会受到欢迎。

【问题讨论】:

    标签: mongodb boolean aggregation-framework


    【解决方案1】:

    试试下面的查询:

    db.collection.aggregate([
      {
        $project: {
          _id: 0,
          name: 1,
          HighEarner: {
            $gt: [
              "$income",
              70000
            ]
          }
        }
      }
    ])
    

    MongoPlayground

    参考:Operator Expressions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 2015-03-13
      • 2019-12-29
      相关资源
      最近更新 更多