I have a schema with an array, and I only want one document from that array. I'm able to do this just fine with aggregate, and while the callback contains the data it doesn't have any of my schema statics or methods, such as it would if I just did a vanilla model.find({}, function(err, documents){})
this.model('servers').aggregate(
[
//unwind the array named 'checks'
{
"$unwind": '$checks'
},
// Sorting pipeline
{
"$sort": {
"checks.date": -1
}
},
// Optionally limit results
{
"$limit": 1
}
],
function(err, documents) {
if (err) throw err
documents.forEach(function(server) {
server.thisMethodCan'tBeFound()
})
}
);
Clearly I'm on the wrong path but I was so excited at not having to return the whole array which could potentially be a million records.