i have a PlaylistShema with an array or musics.
In this array of musics I have an ObjectId
(who ref to a Music collection), an addedAt
and a
You can't sort an array directly in Mongoose. You can clone the data and sort it as a JavaScript object by using toObject
(documentation) and the Array
sort
method:
Playlist.load(function(err, playList) {
var pl = playList.toObject();
pl.musics.sort(function(m1, m2) { return m1.addedAt - m2.addedAt; });
// pl contains the playlist now
});