【问题标题】:MongoDB aggregate array of objects together by object id and count occurencesMongoDB 通过对象 id 和出现次数将对象数组聚合在一起
【发布时间】:2019-06-06 03:38:20
【问题描述】:

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

这就是我的数据对象的样子

[{
  "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
            }
        }
    }
])

电流输出

[
    {
        "_id": 0,
        "count": 1
    },
    {
        "_id": 3,
        "count": 2
    },
    {
        "_id": 5,
        "count": 1
    }
]

【问题讨论】:

  • 请同时显示您的汇总查询..
  • 我认为这不会帮助我远离结果 xD,但我会分享我所拥有的 - 但感谢您的快速回复

标签: mongodb mongoose aggregate


【解决方案1】:

我已将您的记录添加到我的数据库中。发布我一一尝试您的命令。

$unwind 结果类似于 -

> 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 上进行分组以将其投射到投影中。

投影是您在查询中缺少的内容 -

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

您还可以在 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
    }}, 
    {$group: {
        _id:{id:"$id"},
        results: { $addToSet: {answer: "$answer", count: '$count'} }
    }},
    {$project : {
        id: '$_id.id',
        answer: '$results', 
        _id:0
    }})

希望这会有所帮助。

【讨论】:

  • 太棒了!喜欢它,如果我们可以在linkedin上联系,请告诉我,我很喜欢你解决它的方式 - 我最大的感谢
  • 很高兴我能帮上忙。当然,我们可以联系 - in.linkedin.com/in/kravigupta :)
猜你喜欢
  • 2021-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 2017-07-14
  • 2020-07-27
相关资源
最近更新 更多