MongoDB通过对象ID和出现次数将对象数组聚合在一起

时间:2019-01-11 08:21:49

标签: mongodb mongoose aggregate

我正在尝试找出我在做什么错,我收集了以下内容:“ 数据子集”,“ 所需的输出

这是我的数据对象的外观

SELECT RIGHT('0' + CONVERT(VARCHAR(2), MONTH(GETDATE())), 2)

我想要的输出是:

[{
  "survey_answers": [
    {
      "id": "9ca01568e8dbb247", // As they are, this is the key to groupBy
      "option_answer": 5, // Represent the index of the choosen option
      "type": "OPINION_SCALE" // Opinion scales are 0-10 (meaning elleven options)
    },
    {
      "id": "ba37125ec32b2a99",
      "option_answer": 3,
      "type": "LABELED_QUESTIONS" // Labeled questions are 0-x (they can change it from survey to survey)
    }
  ],
  "survey_id": "test"
},
{
  "survey_answers": [
    {
      "id": "9ca01568e8dbb247",
      "option_answer": 0,
      "type": "OPINION_SCALE"
    },
    {
      "id": "ba37125ec32b2a99",
      "option_answer": 3,
      "type": "LABELED_QUESTIONS"
    }
  ],
  "survey_id": "test"
}]

活动查询

[
  {
    id: '9ca01568e8dbb247'
    results: [
      { _id: 5, count: 1 },
      { _id: 0, count: 1 }
    ]
  },
  {
    id: 'ba37125ec32b2a99'
    results: [
      { _id: 3, count: 2 }
    ]
  }
]

当前输出

Model.aggregate([
    {
        $match: {
            'survey_id': survey_id
        }
    },
    {
        $unwind: "$survey_answers"
    },
    {
        $group: {
            _id: "$survey_answers.option_answer",
            count: {
                $sum: 1
            }
        }
    }
])

1 个答案:

答案 0 :(得分:1)

我将您的记录添加到我的数据库中。张贴我一一尝试过您的命令。

$ unwind结果类似于-

<div ng-if="ctrl.currentState == 'employee'">
    <small><a href="/createQuote" title="Change Settings"></a></small>
</div>
<div ng-if="ctrl.currentState == 'fleetmanager'">
        <small><a href="/createQuote/fleetmanager" title="Change Settings"></a></small>
</div>

我也没有添加匹配代码,因为这在您的查询中也可以

分组将为-

> db.survey.aggregate({$unwind: "$survey_answers"})

{ "_id" : ObjectId("5c3859e459875873b5e6ee3c"), "survey_answers" : { "id" : "9ca01568e8dbb247", "option_answer" : 5, "type" : "OPINION_SCALE" }, "survey_id" : "test" }
{ "_id" : ObjectId("5c3859e459875873b5e6ee3c"), "survey_answers" : { "id" : "ba37125ec32b2a99", "option_answer" : 3, "type" : "LABELED_QUESTIONS" }, "survey_id" : "test" }
{ "_id" : ObjectId("5c3859e459875873b5e6ee3d"), "survey_answers" : { "id" : "9ca01568e8dbb247", "option_answer" : 0, "type" : "OPINION_SCALE" }, "survey_id" : "test" }
{ "_id" : ObjectId("5c3859e459875873b5e6ee3d"), "survey_answers" : { "id" : "ba37125ec32b2a99", "option_answer" : 3, "type" : "LABELED_QUESTIONS" }, "survey_id" : "test" }

您可以对> db.survey.aggregate({$unwind: "$survey_answers"},{$group: { _id: { 'optionAnswer': "$survey_answers.option_answer", 'id':"$survey_answers.id"}, count: { $sum: 1}}}) { "_id" : { "optionAnswer" : 0, "id" : "9ca01568e8dbb247" }, "count" : 1 } { "_id" : { "optionAnswer" : 3, "id" : "ba37125ec32b2a99" }, "count" : 2 } { "_id" : { "optionAnswer" : 5, "id" : "9ca01568e8dbb247" }, "count" : 1 } 进行分组以使其投影。

预测是您在查询中所缺少的-

$survey_answers.id

此外,您可以在id上添加组,并将结果添加到集合中。最后的查询是-

> db.survey.aggregate({$unwind: "$survey_answers"},{$group: { _id: { 'optionAnswer': "$survey_answers.option_answer", 'id':'$survey_answers.id'}, count: { $sum: 1}}}, {$project : {answer: '$_id.optionAnswer', id: '$_id.id', count: '$count', _id:0}})

{ "answer" : 0, "id" : "9ca01568e8dbb247", "count" : 1 }
{ "answer" : 3, "id" : "ba37125ec32b2a99", "count" : 2 }
{ "answer" : 5, "id" : "9ca01568e8dbb247", "count" : 1 }

希望这会有所帮助。