在mongodb中对推送数组排序

在mongodb中对推送数组排序,mongodb,mongoose,aggregation-framework,Mongodb,Mongoose,Aggregation Framework,我有这样一个mongodb结果 [ { "_id": { "_id": "57174838afb8eb97ccd409ca", "name": "Yet another position", "description": "description", "code": "11Y-WK" }, "votes": [ { "candidate": { "_id": "56f196

我有这样一个mongodb结果

[
  {
    "_id": {
      "_id": "57174838afb8eb97ccd409ca",
      "name": "Yet another position",
      "description": "description",
      "code": "11Y-WK"
    },
    "votes": [
      {
        "candidate": {
          "_id": "56f19694e84a6bf1b66ad378",
          "surname": "EBIMOBOERE",
          "firstName": "BLESSING",
          "middleName": "OGIONWO",
          "othername": " BAZIGHA OGIONWO",
          "sc_number": "071050"
        },
        "count": 3
      },
      {
        "candidate": {
          "_id": "56f19690e84a6bf1b66aa558",
          "surname": "OIWO",
          "othername": "PETER ODION",
          "sc_number": "034837"
        },
        "count": 2
      },
      {
        "candidate": {
          "_id": "56f19690e84a6bf1b66aa2f3",
          "surname": "FASHOLA",
          "othername": "OLUFUNKE JUMOKE",
          "sc_number": "008243"
        },
        "count": 4
      }
    ],
    "total_count": 9
  },
  {
    "_id": {
      "_id": "571747a8afb8eb97ccd409c7",
      "name": "Test Position",
      "description": "Description",
      "code": "10T-9K"
    },
    "votes": [
      {
        "candidate": {
          "_id": "56f19690e84a6bf1b66aa3b7",
          "surname": "ASAKA",
          "othername": "ISAAC",
          "sc_number": "044660"
        },
        "count": 1
      },
      {
        "candidate": {
          "_id": "56f19690e84a6bf1b66aa6ea",
          "surname": "ADEYEMI",
          "othername": "OLUWAFUNMILZYO",
          "sc_number": "062444"
        },
        "count": 5
      },
      {
        "candidate": {
          "_id": "56f1968fe84a6bf1b66aa03e",
          "surname": "OGWOR",
          "othername": "JAMES",
          "sc_number": "042357"
        },
        "count": 3
      }
    ],
    "total_count": 9
  }
]
我需要按<代码>计数降序排序

下面是返回上述结果的查询。 我在各个阶段都尝试过使用sort,但没有成功

  Vote.aggregate([
{ "$match": { "_poll" : mongoose.mongo.ObjectID(req.query._poll) } },
{
  "$group": {
    "_id": {
      "_position": '$_position',
      "candidate": '$candidate'
    },
    "voteCount": { "$sum": 1 }
  }
},
{
  "$group": {
    "_id": "$_id._position",
    "votes": {
      "$push": {
        "candidate": "$_id.candidate",
        "count": "$voteCount"
      }
    },
    "total_count": { "$sum": "$voteCount" }
  }
},
{ "$sort": { "total_count": -1 } }

我需要插入排序操作,按计数降序排序。

要在聚合期间对数组进行排序,这有点棘手。 所以我想解决的是:

  • 展开阵列
  • 应用排序
  • 重组数组
  • 请查找mongo shell代码,该代码将概述此技术:

    var group = {$group:{_id:"$type", votes:{$push:"$value" }}}
    var unwind={$unwind:"$votes"}
    var sort = {$sort:{"votes":-1}}
    var reGroup = {$group:{_id:"$_id", votes:{$push:"$votes" }}}
    db.readings.aggregate([group,unwind,sort ,reGroup])
    

    欢迎评论

    我打算按计数排序!我可以执行var sort={$sort:{“voates.count”:-1}}???@Radiumrasheed:我的代码片段是一种示例-根据需要修改和调整