【问题标题】:matching two string object fields in mongo aggregation匹配 mongo 聚合中的两个字符串对象字段
【发布时间】:2015-05-28 05:51:24
【问题描述】:
> 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 匹配(即数组中的第一项访问)并返回其计数。还想返回只有一个数组元素的访问次数数组。

我该怎么做?

【问题讨论】:

  • 如果您比较 cur.pathnamevisits[0].url,查看您的示例文档,它总是会产生 False。你是说cur.hrefvisits[0].url 吗?
  • 我需要访问数组中的第一个 url 值,表示第一个索引处的 url 值。像 JS 中的访问[0].url
  • 您可以在投影中使用$slice 运算符,或者查看此answer 在聚合中进行操作。

标签: node.js mongodb


【解决方案1】:

当前数组位置运算符不可用于聚合。有一个未解决的未解决问题与之相关: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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多