This is the Schema:
var userschema = new mongoose.Schema({
user: String,
messages: [{
title: String,
author: String,
message: String,
date: { type: Date, default: Date.now },
}],
});
I'm trying to show all the users' message in a single page ordered by date. I know how for example show all the messages of a specific user, but if I have multiple users, I don't know how to classificate their message by date using mongoose. Is there any way to do this...?
Thank's advance!
Use the aggregation framework and $unwind the messages array so that you can $sort them all by date:
Users.aggregate([
{$unwind: '$messages'},
{$sort: {'messages.date': 1}}
], function (err, result) {
console.log(result);
}