Matching fields of two string objects in mongo aggregation

> 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"
        }
    ]
}

I want to map the value cur.pathnameto visits[0].url(i.e. the first element in an array visit) and return its score. It is also necessary to return an array of count visits, which has only one element of the array.

How can i do this?

+4
source share
1 answer

The array position operator is currently not available for aggregation. This includes an unresolved open issue: https://jira.mongodb.org/browse/SERVER-4588 and https://jira.mongodb.org/browse/SERVER-4589 .

$first . , . ( , cur.href visits[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
}
+4

All Articles