Home > Net >  When I try comparing dates in Mongoose, I get Mongoose Server Error, why?
When I try comparing dates in Mongoose, I get Mongoose Server Error, why?

Time:09-13

I am trying to get documents from mongoose collection on a date criteria. But I get following error.

"MongoServerError: FieldPath field names may not start with '$'. Consider using $getField or $setField."

My code is ,

   const getByDateRange = async (user_id,startDate,endDate) => {
    //var doc = await expense.find({user_id:user_id});
    var docsByDateRange = await expense.find({user_id:user_id},{ date_of_expense: { $gte: startDate, $lte: endDate } })
   return docsByDateRange;
}

What is the meaning of the error? Where am i going wrong?

CodePudding user response:

in the find method, the code inside the second curly brace remove and move then to the first. we can write our filter in first curly

*const getByDateRange = async (user_id, startDate, endDate) => {
  var docsByDateRange = await expense.find({
    user_id: user_id,
    date_of_expense: { $gte: startDate, $lte: endDate },
  });
  return docsByDateRange;
};*
  • Related