简体   繁体   中英

Retrieving different aggregated fields with mongoose

I am trying to wrap my head around the query which I am trying to make with mongoose on Node JS. Here is my dataset:

{"_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"}}

What I am trying to do is get these values:

  1. The most repeated city in collection
  2. The average of bad_incidents
  3. The maximum value of good_incidents
  4. Maximum salary where there are no bad_incidents

I am trying to wrap my head around how I can do this in one query, because I only need one value per field. I would be glad if somebody would lead me on the right track. No need for full solution

Regards!

You may perform MongoDB aggregation with $facet operator which allows compute several aggregation at once.

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
          ]
        }
      ]
    }
  }
])

MongoPlayground


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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