0

I am using Mongoose to do an aggregate over a collection. The Aggregate query works perfectly well in Mongo Client but somehow mongoose is returning me an empty array.

I was able to pinpoint the problem area which is DATE equality MATCH inside an AND that is causing this empty behavior.

I have the dates in ISODate format and the same string of date works perfectly well in Mongo Client.

const TODAY_START_DATE = moment.utc('2020-09-10').startOf('day').toISOString();
const TODAY_END_DATE = moment.utc('2020-09-10').endOf('day').toISOString();

const todayRecommendations = await recommendationsModel.aggregate().
            lookup({
                from: CcmCore.CCM_GLOBAL_CONST.SCHEMA.RECOMMENDATIONS_PROCESSED,
                localField: 'act_recom_id',
                foreignField: 'act_recom_id',
                as: '_unique'
            })
            .match(
                {
                    $and: [
                        {
                            _unique: { $size: 0 }
                        },
                        {
                            job_timestamp: {
                                $gte:  TODAY_START_DATE,
                                $lte: TODAY_END_DATE
                            }
                        }]
                })
            .exec();

If I remove the Job_timestamp from the MATCH $and, it returns all documents. I am not sure what I am doing wrong here. Here is the same input of the DATE string.

“2020-09-10T00:00:00.000Z”
“2020-09-10T23:59:59.999Z”

Any idea what is going on? Also the same DATE code works fine on another project inside FIND query. Even if I just do a Model.Find with job_timestamp filter it works.

Umer
  • 149
  • 1
  • 1
  • 8
  • 1
    i had similar issue, look at this [question](https://stackoverflow.com/questions/63844762/how-to-prevent-changes-on-timestamp-when-convert-from-string-to-isodate-javascri) will help you. when you filter with date in mongodb it requires date type not string, and this `toISOString()` will return iso date format but in string type . – turivishal Sep 11 '20 at 16:39
  • Thanks. I can seem to make it work. But funny thing is, the same ISOString works in another area with similar query. – Umer Sep 11 '20 at 16:47

0 Answers0