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.