0

The following are my collection Messages

{ "_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 }

I want to get the userId: 59ea8ed214b3710f8faf9764`s Recent messages just like facebook message. and the final result may be like

{ "_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 }

I am new to mongodb and the only thing I know is that I can use aggregate() but no answer about it. Thanks. Ps: recent messages don`t mean that userId: 59ea8ed214b3710f8faf9764 is always fromUserId. It can be toUserId. Depend on the timestamp.

呂裕翔
  • 41
  • 1
  • 6
  • For a specific `"fromUserId"` you would supply that as a "query" and then simply `.sort()` and optionally `.limit()` to the number of "recent" to return. i.e `db.messages.find({ "fromUserId": "59ea8ed214b3710f8faf9764" }).sort({ "timestamp": -1 }).limit(5)` for the 5 most recent. – Neil Lunn Oct 23 '17 at 02:07
  • As a sidenote, you seem to have set a "type" for `"fromUserId"` which is incorrectly marked as a "string". If it looks like an `ObjectId`, then it's best to leave it as an `ObjectId`, and not force it into a "string". Takes a lot more storage, and if this is part of some "relation" then you likely have a type mismatch between this "forienKey" and the "primaryKey" of `_id` in the "related" collection. Different "types" are not equal, so you really should fix that. – Neil Lunn Oct 23 '17 at 02:08
  • @NeilLunn thanks for response I deliberate long time and find the way to achieve the target . – 呂裕翔 Oct 27 '17 at 07:49

0 Answers0