I recently created a schema:
var messageSchema = mongoose.Schema({
sender:String,
recipient:String,
content:String,
messageType: Number,
timestamp: {type: Date, default: Date.now}
});
Next, I defined a model for this schema:
var Message = mongoose.model('Message', messageSchema);
In addition to that, I have an array of IDs/names:
var idArray = [1,2,3,4,5];
My goal is to query and retrieve the latest message document from each sender in the idArray:
var query = Message.find(
{from: { $in: friendsOnline }},
{timestamp: { $last: "$timestamp" }}
});
query.exec(function (err,result){
if(err){ console.log("An error occurred while querying.");}
else{
if(result!=null) {console.log(JSON.stringify(result));}
else{console.log('No results found.');}
}
});
I attempted something similar to the above code, but it didn't produce the desired results.