-1

I have the following object (data model):

const TimeSheetSchema = mongoose.Schema({
    category: {
        type: String,
        require: true
    },
    duration: {
        start: Date,
        end: Date
    },
    isDeleted: {
        type: Boolean,
        default: false
    }
});

How do I destructure the timeSheet object in order to use field "start" in the sort function?

try {
    const timeSheets = await TimeSheet
        .find({isDeleted: false})
        .sort({start: -1});
    res.json(timeSheets);
} catch (err) {
    console.error(err.message);
    res.status(500).send('Server error');
}
Daedalus
  • 7,586
  • 5
  • 36
  • 61
Marcel
  • 9
  • 2

1 Answers1

0

try using dot notation within quotes - https://docs.mongodb.com/manual/reference/method/cursor.sort/#examples

try {
    const timeSheets = await TimeSheet
        .find({isDeleted: false})
        .sort({"duration.start": -1});
    res.json(timeSheets);
} catch (err) {
    console.error(err.message);
    res.status(500).send('Server error');
}
Abhishek
  • 1,302
  • 10
  • 18