MongoDB / Mongoose: find (query) and aggregate ([$ match: query]) results differ when using dates and $ gte / $ lte

I am using Mongoose to build an aggregation pipeline and the match works fine as long as I don't want to match dates using the $gte

and operators $lte

. The weird thing is that if I use a match query in regular find()

it works as expected:

var query = {
  dueDate: {
    $gte: moment().toISOString() // I've also tried using $date { ... }
  }
};

// finds entries matching the query [..., ...]
Model.find(query, callback);

      

However, aggregation using $match

the same query fails:

var aggregation = [{
  $match: query
}];

// finds no entries (using the same query) []
Model.aggregate(aggregation, callback);

      

Any ideas why this is so?

Many thanks!

+3


source to share


1 answer


when using aggregation remove toISOString () the aggregate does not work with this

it works:



var query = {
  dueDate: {
    $gte: moment()
  }
};

      

+3


source







All Articles