I am currently utilizing the nodejs mongodb native driver for developing a chat application. Within my database, I have a collection named dialog which contains fields for sessionId and dateCreated (timestamp). My objective is to retrieve a list of distinct sessionIds ordered by their latest (max) dateCreated timestamp, with a limit of 20 records.
Below is my current implementation:
db.collection('dialog').distinct('sessionId',
function(err, users){
if(users.length > 10){
users = users.slice(0, 20);
}
console.log("users: " + users);
});
Here are a couple sample documents from the collection:
{
"sessionId" : 455206183,
"msg" : "hi what's up?",
"dateCreated" : ISODate("2015-02-05T20:17:55.418Z"),
"_id" : ObjectId("54d3cff30e3ddb6922fbc2bb")
}, {
"sessionId" : 163220612,
"msg" : "yo",
"dateCreated" : ISODate("2015-02-05T20:18:00.434Z"),
"_id" : ObjectId("54d3cff80e3ddb6922fbc2bc")
}