我在这个例子集合中使用mongo db进行聚合请求:
{
"_id" : ObjectId("5756e3e0f32d0541149c6d6d"),
"host" : "www.mywebsite.com",
"ip" : "0.0.0.0",
"user_agents" : {
"dcc0ed6139b2c38f189c9e3c978c675bf4d5ad09" : { // sha1 of the user agent
"user_agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0",
"created_at" : ISODate("2016-06-07T15:10:26.000Z"),
"updated_at" : ISODate("2016-06-20T12:38:53.000Z")
}
},
"created_at" : ISODate("2016-06-07T15:10:26.000Z"),
"number_visits" : 162,
"updated_at" : ISODate("2016-06-20T12:38:53.000Z"),
"referer" : {
"84063d912731824c07241f459d95b77cf6be85de" : { // sha1 of the referer
"updated_at" : ISODate("2016-06-20T12:38:53.000Z"),
"referer" : "http://subdomain.website.net/",
"created_at" : ISODate("2016-06-20T12:38:53.000Z")
},
"8a85c599e53662be5b4888b1879168b042e52adc" : { // sha1 of the referer
"updated_at" : ISODate("2016-06-20T12:29:32.000Z"),
"created_at" : ISODate("2016-06-20T12:29:32.000Z"),
"referer" : "https://www.google.com"
}
}
}
这是我的收藏中的一个例子,它用一些信息(user_agent, referer if exists, host...
)保存每个请求的IP地址。
我想执行一个aggregate
来保存集合中的latest user agent
和latest referer
(基于用户代理中的updated_at
字段,并且请求aburt)得到这个结果:
{
"_id" : "81.56.17.47",
"latest_referer" : 'mylast/referer/url',
"latest_user_agent" : "the_latest_user_agent",
}
我怎样才能实现这个目标?