使用Aggregation框架创建从SQL到MongoDB的值的层次结构

时间:2012-08-22 23:01:08

标签: mongodb nosql-aggregation

我遇到了一个我正在努力解决的数据转换问题:

假设我有一组问题和答案记录,这些记录来自加载到表格

的mongo中的民意调查

ID PollTitle Creator问题答案答辩人

我想将这些转换为我认为更紧凑的JSON结构:

{
 ID,
 Poll Title,
 Questions : [
    { QuestionTitle, QuestionNumber, Answers : [ 
        { RespondentName, Answer, SubmittedTime }
        ]
    ]
}

这似乎是减少记录数量和代表民意调查的更好方法 - >问题 - >解答。以下是单个民意调查的一些记录示例:

_id Poll ID Poll Title  Creator Question  Quest#  Responder Answer  Response Time
742888  9258    Daily Checkin   Mike    Was it a good meeting   1   John    Yes  8/16
742889  9258    Daily Checkin   Mike    Was it a good meeting   1   Len No   8/16
742890  9258    Daily Checkin   Mike    Do you agree with goal  2   John    Yes  8/16
742891  9258    Daily Checkin   Mike    Do you agree with goal  2   Len Yes  8/16

努力弄清楚如何使用聚合框架在查询语言中做到这一点。

1 个答案:

答案 0 :(得分:0)

您必须在聚合框架中执行多个$ group步骤。

以下是您的示例数据和您将获得的输出的结果:

db.poll.aggregate(
[
    {
        "$group" : {
            "_id" : {
                "ID" : "$ID",
                "title" : "$title",
                "QuestionTitle" : "$question",
                "QuestionNumber" : "$questionNum"
            },
            "answer" : {
                "$push" : {
                    "responder" : "$responder",
                    "Answer" : "$answer",
                    "respTime" : "$respTime"
                }
            }
        }
    },
    {
        "$group" : {
            "_id" : {
                "ID" : "$_id.ID",
                "title" : "$_id.title"
            },
            "Questions" : {
                "$push" : {
                    "QuestionTitle" : "$_id.QuestionTitle",
                    "QuestionNumber" : "$_id.questionNumber",
                    "Answers" : "$answer"
                }
            }
        }
    }
])
{
    "result" : [
        {
            "_id" : {
                "ID" : 9258,
                "title" : "Daily Checkin"
            },
            "Questions" : [
                {
                    "QuestionTitle" : "Do you agree with goal",
                    "Answers" : [
                        {
                            "responder" : "John",
                            "Answer" : "Yes",
                            "respTime" : "8/16"
                        },
                        {
                            "responder" : "Len",
                            "Answer" : "Yes",
                            "respTime" : "8/16"
                        }
                    ]
                },
                {
                    "QuestionTitle" : "Was it a good meeting",
                    "Answers" : [
                        {
                            "responder" : "John",
                            "Answer" : "Yes",
                            "respTime" : "8/16"
                        },
                        {
                            "responder" : "Len",
                            "Answer" : "No",
                            "respTime" : "8/16"
                        }
                    ]
                }
            ]
        }
    ],
    "ok" : 1
}

如果要重命名任何字段或以其他方式转换文档的确切格式,最后可以使用$ project。