I want to sort an array of objects inside my modal and the only way to do it (as I saw) is the following:
...
const notifications = await Notification
.aggregate([
{$match: {to: userId}},
{$unwind: '$messages'},
{$sort: {'messages.createdAt':-1}},
{$group: {
_id: '$_id',
createdAt: {$first: '$createdAt'},
messages: {$push: '$messages'},
}}
])
...
However, I am wondering if there is another way to do it. It seems that I have to do so many operations, even though all I want is to sort an array (grouping for example would make sense if I would actually change anything about the data but I dont). The only other way to do it I found was this:
const notifications = await Notification.findOne({to: userId}).sort({'messages.createdAt': -1})
but that doesnt sort my array in descending order, basically does nothing.
const notificationSchema = new mongoose.Schema({
to: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
messages: [{
title: {
type: String,
required: true
},
body: {
type: String
},
isRead: {
type: Boolean,
default: false
},
createdAt: {
type: Date,
default: new Date()
}
}, ]
},{timestamps: true});