2015-04-02 22 views
5

Tôi đang làm việc trên hệ thống tin nhắn và tôi cần nhận thông báo cuối cùng từ mỗi người dùng đã gửi tin nhắn tới người dùng đã đăng nhập. Tôi có cấu trúc này trong MongoDB:Mongoose - tìm tin nhắn cuối cùng từ mỗi người dùng

[{ 
    "_id": "551bd621bb5895e4109bc3ce", 
    "from": "admin", 
    "to": "user1", 
    "message": "message1", 
    "created": "2015-04-01T11:27:29.671Z" 
}, { 
    "_id": "551bd9acf26208ac1d9b831d", 
    "from": "user1", 
    "to": "admin", 
    "message": "message2", 
    "created": "2015-04-01T11:42:36.936Z" 
}, { 
    "_id": "551bdd6d849d53001dd8a64a", 
    "from": "user1", 
    "to": "user2", 
    "message": "message3", 
    "created": "2015-04-01T11:58:37.858Z" 
}, { 
    "_id": "551bdd99849d53001dd8a64b", 
    "from": "user2", 
    "to": "admin", 
    "__v": 0, 
    "message": "message4", 
    "created": "2015-04-01T11:59:21.005Z" 
}, { 
    "_id": "551bdda1849d53001dd8a64c", 
    "from": "user1", 
    "to": "admin", 
    "__v": 0, 
    "message": "message5", 
    "created": "2015-04-01T11:59:29.971Z" 
}] 

tôi cần để có được lĩnh vực from, messagecreated từ tin nhắn cuối cùng của mỗi người dùng gửi thông điệp tới người dùng đăng nhập. Tôi đã thử sử dụng riêng biệt nhưng nó chỉ trả lại một trường. Tôi có điều này:

Message.find({ 
     to: req.user.username 
    }) 
    .select('message created') 
    .sort('-created') 
    .exec(function (err, messages) { 
     if (err) { 
      return res.status(400).send({ 
       message: getErrorMessage(err) 
      }); 
     } else { 
      res.json(messages) 
     } 
    }); 

nhưng nó trả về tất cả người dùng đã gửi thư cho người dùng đã đăng nhập và tôi chỉ cần người dùng duy nhất và tin nhắn cuối cùng của họ. Có cách nào để làm điều đó với mongoose?

+0

thể trùng lặp của [ Truy vấn Mongodb: Bản ghi mới nhất theo ngày cho mỗi mục] (http://stackoverflow.com/questions/29368141/mongodb-query-latest-record-by-date-for-each-item) – Dineshaws

Trả lời

7

Sử dụng khuôn khổ hợp nơi công đoạn đường ống dẫn của bạn có $match, $sort, $group$project biểu:

Message.aggregate(
    [ 
     // Matching pipeline, similar to find 
     { 
      "$match": { 
       "to": req.user.username 
      } 
     }, 
     // Sorting pipeline 
     { 
      "$sort": { 
       "created": -1 
      } 
     }, 
     // Grouping pipeline 
     { 
      "$group": { 
       "_id": "$from", 
       "message": { 
        "$first": "$message" 
       }, 
       "created": { 
        "$first": "$created" 
       } 
      } 
     }, 
     // Project pipeline, similar to select 
     { 
      "$project": { 
       "_id": 0, 
       "from": "$_id", 
       "message": 1, 
       "created": 1 
      } 
     } 
    ], 
    function(err, messages) { 
     // Result is an array of documents 
     if (err) { 
      return res.status(400).send({ 
       message: getErrorMessage(err) 
      }); 
     } else { 
      res.json(messages) 
     } 
    } 
); 

Nếu req.user.username = "admin", với bộ sưu tập mẫu của bạn thì kết quả là:

{ 
    "result" : [ 
     { 
      "message" : "message4", 
      "created" : "2015-04-01T11:59:21.005Z", 
      "from" : "user2" 
     }, 
     { 
      "message" : "message5", 
      "created" : "2015-04-01T11:59:29.971Z", 
      "from" : "user1" 
     } 
    ], 
    "ok" : 1 
} 
+2

Tuyệt vời, cảm ơn bạn! –

Các vấn đề liên quan