用不同的键重组数组

时间:2018-11-22 07:57:43

标签: mongodb mongodb-query aggregation-framework

我有一个数据结构:

[
            {
                "_id": {
                    "question": "Voluptatem perferendis voluptas ex.",
                    "option": "Vero rerum qui animi quia assumenda."
                },
                "votes_by_gender": [
                    {
                        "user_gender": "F",
                        "total_votes": 1
                    },
                    {
                        "user_gender": "M",
                        "total_votes": 2
                    }
                ]
            },
            {
                "_id": {
                    "question": "Voluptatem perferendis voluptas ex.",
                    "option": "Suscipit iste molestias est est porro et atque."
                },
                "votes_by_gender": [
                    {
                        "user_gender": "M",
                        "total_votes": 2
                    }
                ]
            }, {...} , ...
]

我需要重新组合它。我需要在路线顶部添加一个“问题”,其他所有条件均相同。所以,我正在尝试类似的东西:

[
 { _id : '$question', options : [{option, votes_by_gender},{...} ...] },
 ....
]

我该怎么做?

1 个答案:

答案 0 :(得分:1)

$unwind之前,您需要$group

db.collection.aggregate([
  { "$unwind": "$votes_by_gender" },
  { "$group": {
    "_id": "$_id.question",
    "options": {
      "$push": {
        "option": "$_id.option",
        "votes_by_gender": "$votes_by_gender"
      }
    }
  }}
])

输出如下:

{
        "_id" : "Voluptatem perferendis voluptas ex.",
        "options" : [
                {
                        "option" : "Vero rerum qui animi quia assumenda.",
                        "votes_by_gender" : {
                                "user_gender" : "F",
                                "total_votes" : 1
                        }
                },
                {
                        "option" : "Vero rerum qui animi quia assumenda.",
                        "votes_by_gender" : {
                                "user_gender" : "M",
                                "total_votes" : 2
                        }
                },
                {
                        "option" : "Suscipit iste molestias est est porro et atque.",
                        "votes_by_gender" : {
                                "user_gender" : "M",
                                "total_votes" : 2
                        }
                }
        ]
}

当然,如果您只是想将“问题”放在一起而不重新组合数组内容,那么您只需$group

db.collection.aggregate([
  { "$group": {
    "_id": "$_id.question",
    "options": {
      "$push": {
        "option": "$_id.option",
        "votes_by_gender": "$votes_by_gender"
      }
    }
  }}
])

输出如下:

{
        "_id" : "Voluptatem perferendis voluptas ex.",
        "options" : [
                {
                        "option" : "Vero rerum qui animi quia assumenda.",
                        "votes_by_gender" : [
                                {
                                        "user_gender" : "F",
                                        "total_votes" : 1
                                },
                                {
                                        "user_gender" : "M",
                                        "total_votes" : 2
                                }
                        ]
                },
                {
                        "option" : "Suscipit iste molestias est est porro et atque.",
                        "votes_by_gender" : [
                                {
                                        "user_gender" : "M",
                                        "total_votes" : 2
                                }
                        ]
                }
        ]
}

但这是数组中的数组,即使您认为自己想要这么做,也不建议这样做。