在Mongo聚合中匹配两个字符串对象字段

4
> db.doc.find().pretty();
{
    "_id" : ObjectId("55669401a5f628a23fed5adc"),
    "cur" : {
        "pathname" : "/blog",
        "href" : "http://www.example.com/blog/"
    },
    "visits" : [
        {
            "url" : "http://www.example.com/blog/",
            "gt_ms" : "1110"
        }
    ]
}
{
    "_id" : ObjectId("556697eba5f628a23fed5add"),
    "cur" : {
        "pathname" : "/twcontroller/insights/login.php",
        "href" : "http://www.example.com/twcontroller/insights/login.php"
    },
    "visits" : [
        {
            "url" : "http://www.example.com/twcontroller/insights/login.php",
            "gt_ms" : "990"
        }
    ]
}
{
    "_id" : ObjectId("556697eba5f628a23fed5ade"),
    "cur" : {
        "pathname" : "/",
        "href" : "http://www.example.com/"
    },
    "visits" : [
        {
            "url" : "http://www.example.com/",
            "gt_ms" : "719"
        },
        {
            "url" : "http://www.example.com/",
            "gt_ms" : "719"
        }
    ]
}
{
    "_id" : ObjectId("556697eba5f628a23fed5adf"),
    "cur" : {
        "pathname" : "/",
        "href" : "http://www.example.com/"
    },
    "visits" : [
        {
            "url" : "http://www.example.com/",
            "gt_ms" : "62"
        },
        {
            "url" : "http://www.example.com/",
            "gt_ms" : "62"
        },
        {
            "url" : "http://www.example.com/",
            "gt_ms" : "62"
        },
        {
            "url" : "http://www.example.com/",
            "gt_ms" : "62"
        },
        {
            "url" : "http://www.example.com/",
            "gt_ms" : "62"
        },
        {
            "url" : "http://www.example.com/",
            "gt_ms" : "62"
        },
        {
            "url" : "http://www.example.com/",
            "gt_ms" : "62"
        }
    ]
}

我希望能够将cur.pathname的值与visits[0].url(即数组visits中的第一个元素)进行匹配,并返回其计数。同时,我还想返回只有一个数组元素的visits数组的计数。

我该如何做到这一点?


1
看了你的样本文档,如果你比较 cur.pathnamevisits[0].url,它总是会返回 False。你是不是想说 cur.hrefvisits[0].url - thegreenogre
我需要访问数组中第一个URL值,也就是JS中visits[0].url的值。 - Afeela Ashraf
您可以在投影中使用$slice运算符,或者查看这个答案来在聚合中完成它。 - thegreenogre
1个回答

4

目前聚合操作中不支持数组位置运算符。有一个未解决的问题与之相关联: https://jira.mongodb.org/browse/SERVER-4588https://jira.mongodb.org/browse/SERVER-4589

您将需要使用$first运算符来投影数组的第一个元素。以下查询应该可以给您想要的结果。(我假设您想比较cur.hrefvisits[0].url。)

db.Testing.aggregate([
    {
        "$project": {
            "hasOneElement": {
                "$cond": [
                    { "$eq": [{"$size": "$visits"}, 1] },
                    1,
                    0
                ]
            },
            "href": "$cur.href",
            "visits.url" : 1
        }
    },
    {'$unwind': '$visits'},
    {'$group': {
        '_id': "$_id",
        'href': {'$first': '$href'}, 
        'visits': {'$first': '$visits'},
        'hasOneElement': {'$first': '$hasOneElement'} 
        }
    },
    {
        '$project': {
            'hasOneElement': 1,
            "isMatch": {
                "$cond": [
                    { "$eq": ["$href", "$visits.url"] },
                    1,
                    0
                ]
            }
        }
    },
    {
        '$group': {
            '_id' : 'test',
            'visit_count' : {'$sum' : '$isMatch' },
            'oneelement_count' : {'$sum' : "$hasOneElement" }
        }
    }
])

提供的示例文档的结果。
{
    "result" : [ 
        {
            "_id" : "test",
            "visit_count" : 4,
            "oneelement_count" : 2
        }
    ],
    "ok" : 1
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接