2015-06-21 45 views
3

我試圖查詢mongoDB以獲取數據聚合。這裏是我的文檔:如何從聚合結果中通過用戶標識獲取用戶對象

關係:

{ 
    "_id" : 1, 
    "from" : "a", 
    "to" : "b", 
    "message" : "a to b", 
    "createdAt" : ISODate("2015-06-06T16:42:32.789Z"), 
    "updatedAt" : ISODate("2015-06-06T16:42:32.789Z") 
} 
{ 
    "_id" : 2, 
    "from" : "a", 
    "to" : "c", 
    "message" : "a to c", 
    "createdAt" : ISODate("2015-06-06T16:43:32.789Z"), 
    "updatedAt" : ISODate("2015-06-06T16:43:32.789Z") 
} 
{ 
    "_id" : 3, 
    "from" : "b", 
    "to" : "c", 
    "message" : "b to c", 
    "createdAt" : ISODate("2015-06-06T16:44:32.789Z"), 
    "updatedAt" : ISODate("2015-06-06T16:44:32.789Z") 
} 
{ 
    "_id" : 4, 
    "from" : "a", 
    "to" : "c", 
    "message" : "a to c2", 
    "createdAt" : ISODate("2015-06-06T16:45:32.789Z"), 
    "updatedAt" : ISODate("2015-06-06T16:45:32.789Z") 
} 
{ 
    "_id" : 5, 
    "from" : "b", 
    "to" : "c", 
    "message" : "b to c2", 
    "createdAt" : ISODate("2015-06-06T16:46:32.789Z"), 
    "updatedAt" : ISODate("2015-06-06T16:46:32.789Z") 
} 

用戶:

{ 
    "_id" : 'a', 
    "name" : "q", 
    "createdAt" : ISODate("2015-06-14T17:20:27.288Z"), 
    "updatedAt" : ISODate("2015-06-15T14:24:30.383Z") 
} 
{ 
    "_id" : 'b', 
    "name" : "e", 
    "createdAt" : ISODate("2015-06-14T17:20:29.288Z"), 
    "updatedAt" : ISODate("2015-06-15T14:24:3.383Z") 
} 
{ 
    "_id" : 'c', 
    "name" : "t", 
    "createdAt" : ISODate("2015-06-14T17:20:28.288Z"), 
    "updatedAt" : ISODate("2015-06-15T14:24:38.383Z") 
} 

我已經試過這樣:

db.collection.aggregate([ 
    { 
     "$sort": { 
      "updatedAt": -1 
     } 
    }, 
    { 
     "$group": { 
      "_id": { 
       "to": "$to", 
       "from": "$from"     
      }, 
      "id": { "$first": "$_id" }, 
      "message": { "$first": "$message" }, 
      "createdAt": { "$first": "$createdAt" }, 
      "updatedAt": { "$first": "$updatedAt" } 
     } 
    }, 
    { 
     "$project": { 
      "_id" : 0, 
      "id": 1, 
      "from" : "$_id.from", 
      "to": "$_id.to", 
      "message": 1, 
      "createdAt": 1, 
      "updatedAt": 1 
     } 
    } 
]); 

我有這樣的:

{ 
    "_id" : 1, 
    "from" : "a", 
    "to" : "b", 
    "message" : "a to b", 
    "createdAt" : ISODate("2015-06-06T16:42:32.789Z"), 
    "updatedAt" : ISODate("2015-06-06T16:42:32.789Z") 
} 
{ 
    "_id" : 4, 
    "from" : "a", 
    "to" : "c", 
    "message" : "a to c2", 
    "createdAt" : ISODate("2015-06-06T16:45:32.789Z"), 
    "updatedAt" : ISODate("2015-06-06T16:45:32.789Z") 
} 
{ 
    "_id" : 5, 
    "from" : "b", 
    "to" : "c", 
    "message" : "b to c2", 
    "createdAt" : ISODate("2015-06-06T16:46:32.789Z"), 
    "updatedAt" : ISODate("2015-06-06T16:46:32.789Z") 
} 

現在,我想要獲得一個文件與最近來回組合。例如:

{ 
     "_id" : 1, 
     "from" :{ 
      "_id" : 'a', 
      "name" : "q", 
      "createdAt" : ISODate("2015-06-14T17:20:27.288Z"), 
      "updatedAt" : ISODate("2015-06-15T14:24:30.383Z") 
     }, 
     "to" : { 
      "_id" : 'b', 
      "name" : "e", 
      "createdAt" : ISODate("2015-06-14T17:20:29.288Z"), 
      "updatedAt" : ISODate("2015-06-15T14:24:3.383Z") 
     }, 
     "message" : "a to b", 
     "createdAt" : ISODate("2015-06-06T16:42:32.789Z"), 
     "updatedAt" : ISODate("2015-06-06T16:42:32.789Z") 
} 
{ 
     "_id" : 4, 
     "from" :{ 
      "_id" : 'a', 
      "name" : "q", 
      "createdAt" : ISODate("2015-06-14T17:20:27.288Z"), 
      "updatedAt" : ISODate("2015-06-15T14:24:30.383Z") 
     }, 
     "to" : { 
      "_id" : 'c', 
      "name" : "t", 
      "createdAt" : ISODate("2015-06-14T17:20:28.288Z"), 
      "updatedAt" : ISODate("2015-06-15T14:24:38.383Z") 
     }, 
     "message" : "a to c2", 
     "createdAt" : ISODate("2015-06-06T16:45:32.789Z"), 
     "updatedAt" : ISODate("2015-06-06T16:45:32.789Z") 
} 
{ 
     "_id" : 5, 
     "from" : { 
      "_id" : 'b', 
      "name" : "e", 
      "createdAt" : ISODate("2015-06-14T17:20:29.288Z"), 
      "updatedAt" : ISODate("2015-06-15T14:24:3.383Z") 
     }, 
     "to" : { 
      "_id" : 'c', 
      "name" : "t", 
      "createdAt" : ISODate("2015-06-14T17:20:28.288Z"), 
      "updatedAt" : ISODate("2015-06-15T14:24:38.383Z") 
     }, 
     "message" : "b to c2", 
     "createdAt" : ISODate("2015-06-06T16:46:32.789Z"), 
     "updatedAt" : ISODate("2015-06-06T16:46:32.789Z") 
} 

任何幫助代碼表示讚賞。

+0

你不能得到在單個查詢中,這對你來說可以嗎? – bagrat

+0

如果你能給我任何解決這個問題的建議,可能會很好,可能不是單個查詢。僅供參考,即時通訊使用sails.js(水線orm)。 –

+0

正如我從你的問題中得到的,你的聚合查詢非常完美:)你需要的唯一東西就是讓'User'對象而不是'_id'正確嗎? – bagrat

回答

2

因爲MongoDB的不支持的連接,您可以通過使用aggregate()光標forEach()方法循環光標,訪問兩個集合的文件合併兩個集合到一個對象數組,如下面的例子:

var pipeline = [ 
    { 
     "$sort": { 
      "updatedAt": -1 
     } 
    }, 
    { 
     "$group": { 
      "_id": { 
       "to": "$to", 
       "from": "$from"     
      }, 
      "id": { "$first": "$_id" }, 
      "message": { "$first": "$message" }, 
      "createdAt": { "$first": "$createdAt" }, 
      "updatedAt": { "$first": "$updatedAt" } 
     } 
    }, 
    { 
     "$project": { 
      "_id" : 0, 
      "id": 1, 
      "from" : "$_id.from", 
      "to": "$_id.to", 
      "message": 1, 
      "createdAt": 1, 
      "updatedAt": 1 
     } 
    }], 
    cur = db.collection.aggregate(pipeline), 
    result = []; 

cur.forEach(function (doc){ 
    var toUser = db.user.findOne({"_id": doc.to}); 
    var fromUser = db.user.findOne({"_id": doc.from}); 
    doc.to = toUser; 
    doc.from = fromUser; 
    result.push(doc); 
}) 

printjson(result); 

輸出:

[ 
     { 
       "id" : 1, 
       "message" : "a to b", 
       "createdAt" : ISODate("2015-06-06T16:42:32.789Z"), 
       "updatedAt" : ISODate("2015-06-06T16:42:32.789Z"), 
       "from" : { 
         "_id" : "a", 
         "name" : "q", 
         "createdAt" : ISODate("2015-06-14T17:20:27.288Z"), 
         "updatedAt" : ISODate("2015-06-15T14:24:30.383Z") 
       }, 
       "to" : { 
         "_id" : "b", 
         "name" : "e", 
         "createdAt" : ISODate("2015-06-14T17:20:29.288Z"), 
         "updatedAt" : ISODate("2015-06-15T14:24:00Z") 
       } 
     }, 
     { 
       "id" : 4, 
       "message" : "a to c2", 
       "createdAt" : ISODate("2015-06-06T16:45:32.789Z"), 
       "updatedAt" : ISODate("2015-06-06T16:45:32.789Z"), 
       "from" : { 
         "_id" : "a", 
         "name" : "q", 
         "createdAt" : ISODate("2015-06-14T17:20:27.288Z"), 
         "updatedAt" : ISODate("2015-06-15T14:24:30.383Z") 
       }, 
       "to" : { 
         "_id" : "c", 
         "name" : "t", 
         "createdAt" : ISODate("2015-06-14T17:20:28.288Z"), 
         "updatedAt" : ISODate("2015-06-15T14:24:38.383Z") 
       } 
     }, 
     { 
       "id" : 5, 
       "message" : "b to c2", 
       "createdAt" : ISODate("2015-06-06T16:46:32.789Z"), 
       "updatedAt" : ISODate("2015-06-06T16:46:32.789Z"), 
       "from" : { 
         "_id" : "b", 
         "name" : "e", 
         "createdAt" : ISODate("2015-06-14T17:20:29.288Z"), 
         "updatedAt" : ISODate("2015-06-15T14:24:00Z") 
       }, 
       "to" : { 
         "_id" : "c", 
         "name" : "t", 
         "createdAt" : ISODate("2015-06-14T17:20:28.288Z"), 
         "updatedAt" : ISODate("2015-06-15T14:24:38.383Z") 
       } 
     } 
] 
相關問題