how to create an array on the output of a response using aggregate in Mongodb

我是研究僧i 提交于 2020-04-16 02:41:08

问题


I have in my collection a list of objects with this structure:

[
 {
    "country": "colombia",
    "city":"medellin",
    "calification": [
        {
            "_id": 1,
            "stars": 5
        },
        {
            "_id": 2,
            "stars": 3
        }
    ]
},
{
    "country": "colombia",
    "city":"manizales",
    "calification": [
        {
            "_id": 1,
            "stars": 5
        },
        {
            "_id": 2,
            "stars": 5
        }
    ]
},

{
    "country": "argentina",
    "city":"buenos aires",
    "calification": [
        {
            "_id": 1,
            "stars": 5
        },
    ]
},
{
    "country": "perú",
    "city":"cusco",
    "calification": [
        {
            "_id": 3,
            "stars": 3
        },
    ]
  }
]

I am trying to make a filter so that the output is an amount of arrays for each country. this is the example of the output i want.

avg would be result sum 'stars'/ calification.length

{
  "colombia": [
    {
      "city": "medellin",
      "avg": 4,
      "calification": [
        {
          "_id": 1,
          "stars": 5
        },
        {
          "_id": 2,
          "stars": 3
        }
      ]
    },
    {
      "city": "manizales",
      "avg": 5,
      "calification": [
        {
          "_id": 1,
          "stars": 5
        },
        {
          "_id": 2,
          "stars": 3
        }
      ]
    }
  ],
  "argentina": {
    "city": "buenos aires",
    "avg": 5,
    "calification": [
      {
        "_id": 1,
        "stars": 5
      }
    ]
  },
  "peru": {
    "city": "cusco",
    "avg": 4,
    "calification": [
      {
        "_id": 1,
        "stars": 4
      }
    ]
  }
}

I am trying to do this:

    Alcalde.aggregate([

        {
            $addFields: {
                colombia: {
                    "$push": {

                        "$cond": [{ $eq: ["$country", "'Colombia'"] }, true, null]


                    }


                }

            }
        },
        {
            $project: { colombia: "$colombia" }
        }
    ]

how can i do it


回答1:


We can make it more elegant.

MongoDB has $avg operator, let's use it. Also, we can use $group operator to group cities for the same country.

At the end, applying $replaceRoot + $arrayToObject** we transform into desired result.

** it's because we cannot use such expression: {"$country":"$city"}

$replaceRoot                                 $arrayToObject

data : {              {                  [                             {
  "key" : "val",  -->   "key" : "val",     {k:"key", v: "val"},  -->     "key" : "val",
  "key2" : "val2"       "key2" : "val2"    {k:"key2", v: "val2"}         "key2" : "val2"
}                     }                  ]                             }

Try this one:

Alcalde.aggregate([
  {
    $group: {
      _id: "$country",
      city: {
        $push: {
          "city": "$city",
          "avg": { $avg: "$calification.stars"},
          "calification": "$calification"
        }
      }
    }
  },
  {
    $replaceRoot: {
      newRoot: {
        $arrayToObject: [ [{ "k": "$_id", "v": "$city"}] ]
      }
    }
  }
])

MongoPlayground

EDIT: Generic way to populate city inner object

$$ROOT is variable which stores root document
$mergeObjects adds / override fields to final object

Alcalde.aggregate([
  {
    $group: {
      _id: "$country",
      city: {
        $push: {
          $mergeObjects: [
            "$$ROOT",
            {
              "avg": { "$avg": "$calification.stars" }
            }
          ]
        }
      }
    }
  },
  {
    $project: {
      "city.country": 0
    }
  },
  {
    $replaceRoot: {
      newRoot: {
        $arrayToObject: [
          [ { "k": "$_id", "v": "$city" } ]
        ]
      }
    }
  }
])

MongoPlayground



来源:https://stackoverflow.com/questions/61000005/how-to-create-an-array-on-the-output-of-a-response-using-aggregate-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!