繁体   English   中英

用猫鼬检索不同的聚合字段

[英]Retrieving different aggregated fields with mongoose

我试图围绕我试图在 Node JS 上使用 mongoose 进行的查询进行思考。 这是我的数据集:

{"_id":{"$oid":"5e49c389e3c23a1da881c1c9"},"name":"New York","good_incidents":{"$numberInt":"50"},"salary":{"$numberInt":"50000"},"bad_incidents":"30"}
{"_id":{"$oid":"5e49c3bbe3c23a1da881c1ca"},"name":"Cairo","bad_incidents":{"$numberInt":"59"},"salary":{"$numberInt":"15000"}}
{"_id":{"$oid":"5e49c42de3c23a1da881c1cb"},"name":"Berlin","incidents":{"$numberInt":"30"},"bad_incidents":"15","salary":{"$numberInt":"55000"}}
{"_id":{"$oid":"5e49c58ee3c23a1da881c1cc"},"name":"New York","good_incidents":{"$numberInt":"15"},"salary":{"$numberInt":"56500"}}

我想要做的是获得这些值:

  1. 收藏次数最多的城市
  2. bad_incidents 的平均值
  3. good_incidents 的最大值
  4. 没有 bad_incidents 的最高工资

我正在努力思考如何在一个查询中执行此操作,因为每个字段只需要一个值。 如果有人能带领我走上正轨,我会很高兴。 无需完整解决方案

问候!

您可以使用$facet运算符执行MongoDB 聚合,它允许一次计算多个聚合。

db.collection.aggregate([
  {
    $facet: {
      repeated_city: [
        {
          $group: {
            _id: "$name",
            name: {
              $first: "$name"
            },
            count: {
              $sum: 1
            }
          }
        },
        {
          $match: {
            count: {
              $gt: 1
            }
          }
        },
        {
          $sort: {
            count: -1
          }
        },
        {
          $limit: 1
        }
      ],
      bad_incidents: [
        {
          $group: {
            _id: null,
            avg_bad_incidents: {
              $avg: {
                $toInt: "$bad_incidents"
              }
            }
          }
        }
      ],
      good_incidents: [
        {
          $group: {
            _id: null,
            max_good_incidents: {
              $max: {
                $toInt: "$good_incidents"
              }
            }
          }
        }
      ],
      max_salary: [
        {
          $match: {
            bad_incidents: {
              $exists: false
            }
          }
        },
        {
          $group: {
            _id: null,
            max_salary: {
              $max: {
                $toInt: "$salary"
              }
            }
          }
        }
      ]
    }
  },
  {
    $replaceWith: {
      $mergeObjects: [
        {
          $arrayElemAt: [
            "$repeated_city",
            0
          ]
        },
        {
          $arrayElemAt: [
            "$bad_incidents",
            0
          ]
        },
        {
          $arrayElemAt: [
            "$good_incidents",
            0
          ]
        },
        {
          $arrayElemAt: [
            "$max_salary",
            0
          ]
        }
      ]
    }
  }
])

蒙戈游乐场


[
  {
    "_id": null,
    "avg_bad_incidents": 34.666666666666664,
    "count": 2,
    "max_good_incidents": 50,
    "max_salary": 56500,
    "name": "New York"
  }
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM