在Mongodb中按数组元素分组

时间:2019-04-22 15:13:40

标签: mongodb aggregation-framework

我们嵌套了文档,并尝试按数组元素分组。我们的文档结构类似于

/* 1 */
{
    "_id" : ObjectId("5a690a4287e0e50010af1432"),
    "slug" : [ 
        "true-crime-the-10-most-infamous-american-murder-mysteries", 
        "10-most-infamous-american-murder-mysteries"
    ],
    "tags" : [ 
        {
            "id" : "59244aa6b1be5055278e9b5b",
            "name" : "true crime",
            "_id" : "59244aa6b1be5055278e9b5b"
        }, 
        {
            "id" : "5924524db1be5055278ebd6e",
            "name" : "Occult Museum",
            "_id" : "5924524db1be5055278ebd6e"
        }, 
        {
            "id" : "5a690f0fc1a72100110c2656",
            "_id" : "5a690f0fc1a72100110c2656",
            "name" : "murder mysteries"
        }, 
        {
            "id" : "59244d71b1be5055278ea654",
            "name" : "unsolved murders",
            "_id" : "59244d71b1be5055278ea654"
        }
    ]
}

我们想按标签名称查找所有子弹组的列表。我正在尝试跟进,但它得到了结果,但并不准确。每个标签都有数百条记录,但查询却很少。我不确定在这里我在做什么错。 预先感谢。

// Requires official MongoShell 3.6+

db.getCollection("test").aggregate(
    [
        { 
            "$match" : {
                "item_type" : "Post", 
                "site_id" : NumberLong(2), 
                "status" : NumberLong(1)
            }
        },
		{$unwind: "$tags" },         
        { 
            "$group" : {
                "_id" : {
                    "tags᎐name" : "$tags.name",
                    "slug" : "$slug"
                }
            }
        }, 
        { 
            "$project" : {
                "tags.name" : "$_id.tags᎐name",
                "slug" : "$_id.slug",
                "_id" : NumberInt(0)
            }
        }
    ], 
    { 
        "allowDiskUse" : true
    }
);

预期输出为

TagName     Slug
----------
true crime "true-crime-the-10-most-infamous-american-murder-mysteries", 
           "10-most-infamous-american-murder-mysteries"
           "All records where tags true crime"

1 个答案:

答案 0 :(得分:1)

应该使用$push$addToSet来积累{而不是将slug用作_id的一部分,请尝试:

db.test.aggregate([
    {
        $unwind: "$tags"
    },
    {
        $unwind: "$slug"
    },
    {
        $group: {
            _id: "$tags.name",
            slugs: { $addToSet: "$slug" }
        }
    },
    {
        $project: {
            _id: 1,
            slugs: {
                $reduce: {
                    input: "$slugs",
                    initialValue: "",
                    in: {
                        $concat: [ "$$value", ",", "$$this" ]
                    }
                }
            }
        }
    }
])

编辑:要获取以逗号分隔的字符串,可以将$reduce$concat一起使用

输出:

{ "_id" : "murder mysteries", "slugs" : ",10-most-infamous-american-murder-mysteries,true-crime-the-10-most-infamous-american-murder-mysteries" }
{ "_id" : "Occult Museum", "slugs" : ",10-most-infamous-american-murder-mysteries,true-crime-the-10-most-infamous-american-murder-mysteries" }
{ "_id" : "unsolved murders", "slugs" : ",10-most-infamous-american-murder-mysteries,true-crime-the-10-most-infamous-american-murder-mysteries" }
{ "_id" : "true crime", "slugs" : ",10-most-infamous-american-murder- mysteries,true-crime-the-10-most-infamous-american-murder-mysteries" }