MongoDB - 在嵌入式数组中查找单个匹配文档的第一个和最后一个

时间:2017-01-10 10:15:56

标签: mongodb

我有一系列网站,每个网站都包含一系列网站及其正在跟踪的关键字。我还有另一个名为"排名"对于网站中的每个关键字,其中包含排名。到目前为止,这个集合看起来像这样:

{
    "_id" : ObjectId("58503934034b512b419a6eab"),
    "website" : "https://www.google.com",
    "name" : "Google",
    "keywords" : [ 
        "Search", 
        "Websites", 
    ],
    "tracking" : [ 
        {
            "_id" : ObjectId("5874aa1df63258286528598d"),
            "position" : 0,
            "created_at" : ISODate("2017-01-1T09:32:13.831Z"),
            "real_url" : "https://www.google.com",
            "keyword" : "Search"
        }, 
        {
            "_id" : ObjectId("5874aa1ff63258286528598e"),
            "keyword" : "Search",
            "real_url" : "https://www.google.com",
            "created_at" : ISODate("2017-01-2T09:32:15.832Z"),
            "found_url" : "https://google.com/",
            "position" : 3
        }, 
        {
            "_id" : ObjectId("5874aa21f63258286528598f"),
            "keyword" : "Search",
            "real_url" : "https://www.foamymedia.com",
            "created_at" : ISODate("2017-01-3T09:32:17.017Z"),
            "found_url" : "https://google.com/",
            "position" : 2
        }, 

        {
            "_id" : ObjectId("5874aa21f63258286528532f"),
            "keyword" : "Websites",
            "real_url" : "https://www.google.com",
            "created_at" : ISODate("2017-01-1T09:32:17.017Z"),
            "found_url" : "https://google.com/",
            "position" : 1
        }, 

         {
            "_id" : ObjectId("5874aa21f63258286528542f"),
            "keyword" : "Websites",
            "real_url" : "https://www.google.com",
            "created_at" : ISODate("2017-01-1T09:32:17.017Z"),
            "found_url" : "https://google.com/",
            "position" : 2
        }, 

    ]
}

我想做的是:

1)通过关键字

将所有关键字组合在一起

2)找到起始位置(在月初)

3)找到当前位置(截至今天)

所以理论上我希望得到一个像这样的对象:

{
    "_id" : ObjectId("58503934034b512b419a6eab"), 
    "website" : "https://www.google.com",

    "tracking" : [
       {
           "_id" : ObjectId("5874aa1df63258286528598d"), 
           "keyword": "Search",
           "start_position": 0, 
           "todays_position": 3, 

       }, 

        {
           "_id" : ObjectId("5874aa1df63258286528598d"), 
           "keyword": "Website",
           "start_position": 0, 
           "todays_position": 2, 

       }, 


    ]
但是,我对如何在另一个领域进行分组感到困惑。到目前为止,我已尝试过以下内容:

db.getCollection('websites').aggregate([

    {
        $lookup: {
            from: "seo_tracking", 
            localField: "website",
            foreignField: "real_url",
            as: "tracking"
        }
    },

    {
        $match: {
            "_id" : ObjectId("58503934034b512b419a6eab")
        }
    },

    {
        $group: {
            "_id" : "$_id", 
            "keyword" : {
                $first: "$tracking.keyword",
            },
        }
    }

]); 

但这不是按关键字分组,也不能弄清楚如何获得预期值。

1 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情。 $unwind跟踪数组$sort tracking.keywordtracking.created_at后跟$grouptracking.keyword $first$avg获取起始位置,$last获得平均排名,$group获得今天的排名。最后tracking将所有内容汇总回db.website.aggregate([{ $match: { "_id": ObjectId("58503934034b512b419a6eab") } }, { $lookup: { from: "seo_tracking", localField: "website", foreignField: "real_url", as: "tracking" } }, { $unwind: "$tracking" }, { $sort: { "tracking.keyword": 1, "tracking.created_at": -1 } }, { $group: { "_id": "$tracking.keyword", "website": { $first: "$website" }, "website_id": { $first: "$_id" }, "avg_position": { $avg: "$tracking.position" }, "start_position": { $first: "$tracking.position" }, "todays_position": { $last: "$tracking.position" } } }, { $group: { "_id": "$website_id", "website": { $first: "$website" }, "tracking": { $push: { "keyword": "$_id", "avg_position":"$avg_position", "start_position": "$start_position", "todays_position": "$todays_position" } } } }]); 数组。

{{#titleparts:{{fullurl:page}}|3|1}}