2
我喜歡獲取給定用戶和給定位置的最新進入和退出時間戳。集合是這樣的mongoDB獲取陣列的最新日期
{ "ActivityList" : [
{ "type" : "exit",
"timestamp" : Date(1348862537170),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId("4fdeaeeede26fd298262bb80") } },
{ "type" : "entry",
"timestamp" : Date(1348862546966),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId("4fdeaeeede26fd298262bb80") } },
{ "type" : "entry",
"timestamp" : Date(1348870744386),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId("4fdeaf6fde26fd298262bb81") } },
{ "type" : "exit",
"timestamp" : Date(1348878233785),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId("4fdeaf6fde26fd298262bb81") } } ],
"Location" : { "$ref" : "loc",
"$id" : ObjectId("4fd410f0e7e994b59054b824") },
"_id" : ObjectId("4fe8f3c6e7e9ebe3697ee836") }
我想是這樣的,但你的幫助不起作用
db.collection.group(
{
keyf: function(doc) {
return {
location :doc.Location._id,
userid : doc.ActivityList.user._id,
actiontype : doc. ActivityList.type
};
},
reduce: function(obj,prev) {
if (prev.maxdate < obj. ActivityList.timestamp) {
prev.maxdate = obj. ActivityList.timestamp;
}
},
initial: {maxdate:0}
});
感謝。
非常感謝。工作沒有問題。看起來我需要努力學習Mongo 2.2中的聚合函數。再次感謝。 – atandon