aravindnc / mongoose-aggregate-paginate-v2

A cursor based custom aggregate pagination library for Mongoose with customizable labels.
MIT License
131 stars 23 forks source link

Can't match a ISODate range #47

Open Gabrirf opened 1 year ago

Gabrirf commented 1 year ago

I have my documents created with createdAt and updatedAt fields from mongoose feature. I normally add date filters in this way:

const filters = {};
if (from) filters.createdAt = { $gte: new Date(from).toISOString() };
if (to) filters.createdAt = { $lte: new Date(to).toISOString() };
if (from && to) {
  filters.createdAt = {
    $gte: new Date(from).toISOString(),
    $lte: new Date(to).toISOString(),
  };
}

For the aggregate I use:

const options = {};
const filtersPipeline = {
    $match: {
      ...filters,
    },
  };
const aggregateQuery = MyModel.aggregate([
    filtersPipeline,
]);
MyModel.aggregatePaginate(aggregateQuery, { ...options, ...paginationOptions });

It should filter from-to date, but I got an empty array on results

I'm using Node v14 with:

{
    "mongoose": "5.12.9",
    "mongoose-aggregate-paginate-v2": "1.0.42",
    "mongoose-paginate-v2": "1.3.14"
}

This query it's working directly on Robo3t:

db.getCollection('mymodel').aggregate([
    { $match: {
        createdAt: { $gte: ISODate('2022-09-16T09:45:17.000Z') },
    }}
])
dev-stupid-codes commented 1 year ago

I can't even match by _id