在Mongodb中显示最近的聊天消息

时间:2017-10-23 02:00:24

标签: mongodb chat

以下是我的收藏讯息

{ "_id" : ObjectId("59ea8ed614b3710f8faf9765"), "fromUserId" : "59ea8ed214b3710f8faf9764", "message" : "hello", "toUserId" : "59e76718dda66a0e15df4bad", "fromSocketId" : null, "timestamp" : 1508544214 }
{ "_id" : ObjectId("59ea8edf14b3710f8faf9766"), "fromUserId" : "59ea8ed214b3710f8faf9764", "message" : "fuck you asshole", "toUserId" : "59ea8eb614b3710f8faf9763", "fromSocketId" : null, "timestamp" : 1508544223 }
{ "_id" : ObjectId("59ea8f0214b3710f8faf9767"), "fromUserId" : "59ea8eb614b3710f8faf9763", "message" : "ssssss", "toUserId" : "59ea8ed214b3710f8faf9764", "fromSocketId" : null, "timestamp" : 1508544258 }
{ "_id" : ObjectId("59ea8f1014b3710f8faf9768"), "fromUserId" : "59ea8ed214b3710f8faf9764", "message" : "you are so awesome", "toUserId" : "59ea8eb614b3710f8faf9763", "fromSocketId" : null, "timestamp" : 1508544272 }
{ "_id" : ObjectId("59ea8f1f14b3710f8faf9769"), "fromUserId" : "59ea8eb614b3710f8faf9763", "message" : "ee", "toUserId" : "59e76718dda66a0e15df4bad", "fromSocketId" : null, "timestamp" : 1508544287 }
{ "_id" : ObjectId("59eca107fae71114fb270654"), "fromUserId" : "59ea8eb614b3710f8faf9763", "message" : "ss", "toUserId" : "59ea8ed214b3710f8faf9764", "fromSocketId" : null, "timestamp" : 1508679943 }
{ "_id" : ObjectId("59eca10efae71114fb270655"), "fromUserId" : "59ea8ed214b3710f8faf9764", "message" : "hellow", "toUserId" : "59ea8eb614b3710f8faf9763", "fromSocketId" : null, "timestamp" : 1508679950 }

我想获取 userId:59ea8ed214b3710f8faf9764 的最新消息,就像facebook消息一样。 最后的结果可能就像

{ "_id" : ObjectId("59ea8ed614b3710f8faf9765"), "fromUserId" : "59ea8ed214b3710f8faf9764", "message" : "hello", "toUserId" : "59e76718dda66a0e15df4bad", "fromSocketId" : null, "timestamp" : 1508544214 }
{ "_id" : ObjectId("59eca10efae71114fb270655"), "fromUserId" : "59ea8ed214b3710f8faf9764", "message" : "hellow", "toUserId" : "59ea8eb614b3710f8faf9763", "fromSocketId" : null, "timestamp" : 1508679950 }

我是mongodb的新手,我唯一知道的是我可以使用aggregate()但没有答案。谢谢。 Ps:最近的消息并不意味着 userId:59ea8ed214b3710f8faf9764 始终是fromUserId。它可以是toUserId。取决于时间戳。

0 个答案:

没有答案