2015-10-20 59 views
0

我有如下的消息收集:集團通過減少現場取決於變量MongoDB中

{ 
    "_id" : ObjectId("56214d5632001bae07a6e6b3"), 
    "sender_id" : 8, 
    "receiver_id" : 2, 
    "content" : "fdgfd", 
    "state" : 1, 
    "timestamp" : 1445023062899.0000000000000000 
}, 
{ 
    "_id" : ObjectId("56214d5c32001bae07a6e6b4"), 
    "sender_id" : 2, 
    "receiver_id" : 8, 
    "content" : "fasfa", 
    "state" : 1, 
    "timestamp" : 1445023068443.0000000000000000 
}, 
{ 
    "_id" : ObjectId("56214d8032001bae07a6e6b5"), 
    "sender_id" : 2, 
    "receiver_id" : 8, 
    "content" : "dfdsfds", 
    "state" : 1, 
    "timestamp" : 1445023104363.0000000000000000 
}, 
{ 
    "_id" : ObjectId("56214d8032001bae07a6e6b6"), 
    "sender_id" : 2, 
    "receiver_id" : 8, 
    "content" : "fdsf", 
    "state" : 1, 
    "timestamp" : 1445023104825.0000000000000000 
}, 
{ 
    "_id" : ObjectId("56214d8132001bae07a6e6b7"), 
    "sender_id" : 2, 
    "receiver_id" : 8, 
    "content" : "sfsdfs", 
    "state" : 1, 
    "timestamp" : 1445023105436.0000000000000000 
}, 
{ 
    "_id" : ObjectId("56214d8132001bae07a6e6b8"), 
    "sender_id" : 2, 
    "receiver_id" : 8, 
    "content" : "f", 
    "state" : 1, 
    "timestamp" : 1445023105963.0000000000000000 
}, 
{ 
    "_id" : ObjectId("56214d8432001bae07a6e6b9"), 
    "sender_id" : 2, 
    "receiver_id" : 8, 
    "content" : "qwqwqwq", 
    "state" : 1, 
    "timestamp" : 1445023108202.0000000000000000 
}, 
{ 
    "_id" : ObjectId("56214db032001bae07a6e6ba"), 
    "sender_id" : 9902, 
    "receiver_id" : 2, 
    "content" : "fsafa", 
    "state" : 1, 
    "timestamp" : 1445023152297.0000000000000000 
} 

我試圖讓已被短信與用戶2的所有獨特的用戶的ID,與上次內容一起信息。所以結果應該是:

[ { user: 8, lastContent: "qwqwqwq" }, { user: 9902, lastContent: "fsafa" } ] 

到現在爲止,我有以下代碼:

db.getCollection('messenger').group({ 
keyf: function(doc) { 
    return { user: doc.user }; 
}, 
cond: { 
    $or : [ 
     { sender_id : 2 }, 
     { receiver_id : 2 } 
    ] 
}, 
reduce: function(curr, result) { 
    result.user = (curr.sender_id == 2 ? curr.receiver_id : curr.sender_id); 
    result.content = curr.content; 
}, 
initial: { } }) 

但我只得到了最後一個ID。結果:

{ 
"0" : { 
    "user" : 9902.0000000000000000, 
    "content" : "fsafa" 
} } 

任何人都可以幫助我嗎?

回答

1

您需要使用.aggregate()方法。您需要使用$match運算符來縮小流水線中的文檔大小,該運算符會過濾出receiver_id不等於2的所有文檔。之後,您需要$sort您的文件由timestamp降序排列,這將幫助我們獲得發送最後一條消息的content。現在進入$group階段,您將文檔分組並使用$addToSet運算符,該運算符返回不同的sender_id和不同的receiver_id$last運算符的數組以獲取最後一條消息內容。現在得到user_ids,我們需要聯合使用sender_idreceiver_id,我們可以使用$setUnion算子得到$project離子。

db.messenger.aggregate([ 
    { "$match": { 
     "$or": [ 
      { "sender_id": 2 }, 
      { "receiver_id": 2 } 
     ] 
    }}, 
    { "$sort": { "timestamp": 1 } }, 
    { "$group": { 
     "_id": null, 
     "receiver_id": { 
      "$addToSet": { "$receiver_id" } 
     }, 
     "sender_id": { 
      "$addToSet": { "$sender_id" } 
     }, 
     "lastContent": { "$last": "$content" } 
    }}, 
    { "$project": { 
     "_id": 0, 
     "lastContent": 1, 
     "user_ids": { 
      "$setUnion": [ 
       "$sender_id", 
       "$receiver_id" 
      ] 
     } 
    }} 
]) 

將返回:

{ "lastContent" : "fsafa", "user_ids" : [ 9902, 2, 8 ] } 

現在,如果你想要的是不同的用戶一起與用戶2他們最後的內容信息,那麼這就是:

db.messenger.aggregate([ 
    { "$match": { 
     "$or": [ 
      { "sender_id": 2 }, 
      { "receiver_id": 2 } 
     ] 
    }}, 
    { "$sort": { "timestamp": 1 } }, 
    { "$group": { 
     "_id": { 
      "sender": "$sender_id", 
      "receiver": "$receiver_id" 
     }, 
     "lastContent": { 
      "$last": "$content" 
     }, 
     "timestamp": { "$last": "$timestamp" }, 
     "sender": { "$addToSet": "$sender_id" }, 
     "receiver": { "$addToSet": "$receiver_id" } 
    }}, 
    { "$project": { 
     "_id": 0, 
     "user": { 
      "$setDifference": [ 
       { "$setUnion": [ "$sender", "$receiver" ] }, 
       [ 2 ] 
      ] 
     }, 
     "lastContent": 1, 
     "timestamp": 1 
    }}, 
    { "$unwind": "$user" }, 
    { "$sort": { "timestamp": 1 } }, 
    { "$group": { 
     "_id": "$user", 
     "lastContent": { "$last": "$lastContent" } 
    } } 
]) 

其中產量:

{ "_id" : 9902, "lastContent" : "fsafa" } 
{ "_id" : 8, "lastContent" : "qwqwqwq" }