获取一段时间内的平均值

时间:2017-01-10 13:46:15

标签: mongodb mongodb-query aggregation-framework

我有以下系列:

{
        "_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" : "Page",
                "real_url" : "https://www.google.com",
                "created_at" : ISODate("2017-01-1T09: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-2T09:32:17.017Z"),
                "found_url" : "https://google.com/",
                "position" : 2
            }, 

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

我想要做的是将所有关键字组合在一起,并计算某一特定日期内该特定日期的平均值。

所以,让我们举个例子:

在2017-01-01至2017-01-31之间跟踪以下关键字:

2017-01-01:
  'Seach' => 1, 
  'Page'  => 3, 
Average = 2 

2017-01-02: 
   'Search' => 4, 
   'Page'  => 6,
 Average = 5

....

所以在最终结果中,我将完成(在这种情况下):

{
    "_id" : ObjectId("5874dccb9cd90425e41b7c54"),
    "website" : "www.google.com", 
    "averages" : [
       "2", 
       "5"

    ]
}

1 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情。

$unwind跟踪数组后面是$sort上的tracking.keywordtracking.created_at $group day之后的所有类别的平均值。最终$group将所有当天的平均值推送到网站的数组中。

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: {
           $dayOfMonth: "$tracking.created_at"
         },
        "website": {
            $first: "$website"
        },
        "website_id": {
            $first: "$_id"
        },
        "averageByDay": {
            $avg: "$tracking.position"
        }
    }
}, {
    $group: {
        "_id": "$website_id",
        "website": {
            $first: "$website"
        },
        "average": {
            $push: "$averageByDay"
        }
    }
}]);