基于mongodb字段的公共值进行组收集

时间:2019-12-05 15:33:19

标签: javascript node.js mongodb

我的收藏看起来像这样

{
"email" : "tp@sd.com",
"subscriptions" : [
                {
                        "default" : false,
                        "interval" : "weekly",
                        "tags" : [
                                {
                                        "name" : "group-1",
                                        "value" : "g1-value-1"
                                },
                                {
                                        "name" : "group-1",
                                        "value" : "g1-value-2"
                                },
                                {
                                        "name" : "group-2",
                                        "value" : "g2-value-1"
                                },
                                {
                                        "name" : "group-3",
                                        "value" : "g3-value-1"
                                },
                                {
                                        "name" : "group-3",
                                        "value" : "g3-value-2"
                                }
                        ]
},
{
"email":"lol@xyz.com",
"subscriptions" : [
                {
                        "default" : false,
                        "interval" : "weekly",
                        "tags" : [
                                {
                                        "name" : "group-1",
                                        "value" : "g1-value-2"
                                },
                                {
                                        "name" : "group-1",
                                        "value" : "g1-value-1"
                                },
                                {
                                        "name" : "group-2",
                                        "value" : "g2-value-1"
                                },
                                {
                                        "name" : "group-2",
                                        "value" : "g2-value-3"
                                },
                                {
                                        "name" : "group-3",
                                        "value" : "g3-value-1"
                                }
                        ]
}

}

我要将其分组为包含电子邮件和公共组值的对象数组。

例如,用户1的g1-value-1值和用户2的g1-value-1值也可能与其他值一起。但是,这些用户应与他们的标签一起分组。我该如何实现?

1 个答案:

答案 0 :(得分:1)

令人惊讶的是,这是一个非常简单的查询:

db.collection.aggregate([
    {
        $unwind: "$subscriptions"
    },
    {
        $unwind: "$subscriptions.tags"
    },
    {
        $group: {
            _id: "$subscriptions.tags.value",
            emails: {$addToSet: "$email"}
        }
    }
]); 

结果将如下所示:

[
    {
        _id: "g1-value-1",
        emails: [
            "tp@sd.com",
            "lol@xyz.com"
        ]
    },
    {
        _id: "g1-value-2",
        emails: [
            "tp@sd.com",
            "lol@xyz.com"
        ]
    },
    {
        _id: "g2-value-1",
        emails: [
            "tp@sd.com",
            "lol@xyz.com"
        ]
    },
    {
        _id: "g2-value-3",
        emails: [
            "lol@xyz.com"
        ]
    },
    {
        _id: "g3-value-1",
        emails: [
            "tp@sd.com",
            "lol@xyz.com"
        ]
    },
    {
        _id: "g3-value-2",
        emails: [
            "tp@sd.com",
        ]
    },
]