Mongoose - Is there a way to more elegantly filter an array in a document?

I finished the API Exercise Tracker challenge but wondering if there’s a more elegant way to write the code that filters for dates of the exercise…?

This is my Schema - exercises is in an Array

 let profileSchema = new Schema({
  username: { type: String, trim: true, default: "" },
  exercises: [
      description: { type: String, trim: true, default: "" },
      duration: { type: Number, trim: true, default: "" },
      date: { type: Date, trim: true, default: "" }

This is code works but not as elegant as it could be:

app.get("/api/exercise/log/:userId", async (req, res) => {
    let id = req.params.userId,
      fromDate = new Date(req.query.from),
      toDate = new Date(,
      limit = req.query.limit;
  try {
/*--- How to make this more elegant? ---*/
    let profile = await Profile.findById(id); 
    profile.exercises = profile.exercises.filter(x => {
      return (>=fromDate &&<=toDate); 
    profile.exercises = profile.exercises.slice(0,limit);
/*--- How to make this more elegant? ---*/
  } catch {
    return new Error(`Error showing profile for id ${id}.`);

How can I write it more elegantly using Mongoose? Below is what I came up with but it doesn’t work.

let profile = await Profile.find({"_id": id}, {"": {$gte: fromDate, $lte: toDate}}); // somehow also needs to select only 'limit' number of dates...

You could simply append limit method if i remember correctly^^
Something like:

let profile = await Profile
.find({"_id": id}, "": {$gte: fromDate, $lte: toDate}})

Btw this is mongo stuff, mongoose doesn’t play a role here if i am not mistaken^^

Thanks. I’m not sure why but there’s something about the line I wrote that doesn’t work. I had an extra } at the end that I edited and removed too.

Also, the limit would only be for the exercises.dates but not the full profiles, so I think the way to limit it would have to be done differently…?

.find({"_id": id}, "": {$gte: fromDate, $lte: toDate}).limit(5)

Passing these arguments to the find method will return all the records where the id is equal to the one you pass down, then from these records it will strip out all the fields but the one. Eventually it limits the number of the records it returns based on limit method. If you want to return the full profiles, using exercise.dates as criteria then you should write it inside the first parenthesys:

.find({"_id": id, "": {$gte: fromDate, $lte: toDate}}).limit(5)