聚合标签regex



我正在寻找一种方法来计算存在于文档中的标签数量。

数据如下:

[
    {
        "_id": ObjectId("...."),
        "tags": ["active-adult", "active-tradeout"]
    },
    {
        "_id": ObjectId("...."),
        "tags": ["active-child", "active-tradeout", "active-junk-tag"]
    },
    {
        "_id": ObjectId("...."),
        "tags": ["inactive-adult"]
    }
]

我希望聚合的结果是这样的:

[
    {
        "_id": "active",
        "total": 2,
        "subtags": {
            "adult": 1,
            "child": 1,
            "tradeout": 2,
            "junk-tag": 1
        }
    },
    {
        "_id": "inactive",
        "total": 1,
        "subtags": {
            "adult": 1
        }
    }
]

我知道我可以计数标签,但我正在寻找regex

db.User.aggregate([
    {$unwind: "$tags"},
    {$group: {_id: "$tags", total: {$sum: 1}}}
])

您可以使用$substr$cond操作符进行一点字符串处理以获得所需的结果(不需要regex)。这将需要MongoDB 2.6+:

db.User.aggregate([
    { $unwind : "$tags"},
    { $project : { 
        tagType : { 
            $cond : { 
                if : { $eq : [ { $substr : [ "$tags", 0, 6] }, "active" ]}, 
                then: "active", 
                else: "inactive"}
            }, 
        tag: {
            $cond : { 
                if : { $eq : [ { $substr : [ "$tags", 0, 6] }, "active" ]}, 
                then: { $substr : ["$tags", 7, -1]}, 
                else: { $substr : ["$tags", 9, -1]}}
            }
    }},
    { $group : { _id : {tagType : "$tagType", tag: "$tag"} , 
                 total: { $sum: 1}}},
    { $group : { _id : "$_id.tagType", 
                subtags: { $push : {tag : "$_id.tag", total: "$total"}},
                total: { $sum : "$total"}}}
]);

这个查询的结果将是:

{
    "_id" : "inactive",
    "subtags" : [
        {
            "tag" : "adult",
            "total" : 1
        }
    ],
    "total" : 1
}
{
    "_id" : "active",
    "subtags" : [
        {
            "tag" : "junk-tag",
            "total" : 1
        },
        {
            "tag" : "child",
            "total" : 1
        },
        {
            "tag" : "tradeout",
            "total" : 2
        },
        {
            "tag" : "adult",
            "total" : 1
        }
    ],
    "total" : 5
}
编辑:

我刚刚注意到,结果中的总数是计算标签的总数,而不是至少有一个活动标签的文档的数量。这个查询将给出您想要的确切输出,尽管稍微复杂一些:

db.User.aggregate([
    /* unwind so we can process each tag from the array */
    { $unwind : "$tags"},
    /* Remove the active/inactive strings from the tag values 
       and create a new value tagType */
    { $project : { 
        tagType : { 
            $cond : { 
                if : { $eq : [ { $substr : [ "$tags", 0, 6] }, "active" ]}, 
                then: "active", 
                else: "inactive"}
        }, 
        tag: {
            $cond : { 
                if : { $eq : [ { $substr : [ "$tags", 0, 6] }, "active" ]}, 
                then: { $substr : ["$tags", 7, -1]}, 
                else: { $substr : ["$tags", 9, -1]}}
        }
    }},
    /* Group the documents by tag type, so we can 
       find num. of docs by tag type (total) */
    { $group : { _id : "$tagType", 
                 tags :{ $push : "$tag"}, 
                 docId :{ $addToSet : "$_id"}}},
    /* project the values so we can get the 'total' for tag type */
    { $project : { tagType : "$_id", 
                   tags : 1, 
                   "docTotal": { $size : "$docId" }}},
    /* we must unwind to get total count for each tag */
    { $unwind : "$tags"}, 
    /* sum the tags by type and tag value */
    { $group : { _id : {tagType : "$tagType", tag: "$tags"} , 
                 total: { $sum: 1}, docTotal: {$first : "$docTotal"}}},
    /* finally group by tagType so we can get subtags */
    { $group : { _id : "$_id.tagType", 
                 subtags: { $push : {tag : "$_id.tag", total: "$total"}},
                 total: { $first : "$docTotal"}}}
]);

最新更新