I've got these Mongoose Schemas:
var Thread = new Schema({
title: String, messages: [Message]
});
var Message = new Schema({
date_added: Date, author: String, text: String
});
How do you return all Threads with their latest Message subdocument (limit 1) ?
Currently, I'm filtering the Thread.find()
results on the server side but I'd like to move this operation in MongoDb using aggregate()
for performance matters.
You can use
$unwind
,$sort
, and$group
to do this using something like: