I have this mongoose schema where I want to sort menus items according to their createdAt field
user:
...
menus: [
{
name: { type: String },
recipes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Recipe',
},
],
image: {
url: { type: String },
type: {
type: String,
enum: avatarTypes,
}
},
createdAt: {
type: Date,
required: true,
default: Date.now
}
},
],
and in nodeJs when I fetch them I want to sort them according the createdAt in the menus (latest first)
and this is my code but it's not working
const menus = await User.findOne({
_id: id,
}).sort('-menus.createdAt')