2

My $lte (body.to) date: 2019-04-24T15:42:16.472Z My $gte (body.from) date: 2019-03-25T15:42:16.472Z

My aggregation match stage:

  {
      $match: {
        'workflow.site.completedAt': {
          $exists: true
          // $lte: body.from
          // $gte: body.to
        }
      }
    },

if I just have that exists there, I get:

[ { _id: null, date: 2019-04-16T14:56:36.738Z, count: 8450 } ]

I'm grouping using $last the last date object and that's how the date is formatted in the DB. So the data stored in DB is using $date and then it's an ISO string format.

Running the match stage in any combination, even just having LTE on today, I can't match/find any documents.

Data in the actual database:

 "completedAt": {
                    "$date": "2018-09-19T14:45:29.419Z"
                },
SebastianG
  • 8,563
  • 8
  • 47
  • 111
  • Are you sure that both `body.from` and `date` from your documents are the same type ISODate ? – mickl Apr 24 '19 at 17:38
  • @mickl I'm not at all -- that's why I have logged them on the console and posted them here at the top; Judging by their structure -- they seem to be a match between what's in the database (posted here as well) – SebastianG Apr 24 '19 at 17:46
  • The thing is that MongoDB will check types before checking values so I suppose you might be comparing a string with ISODate but it's just a guess – mickl Apr 24 '19 at 17:50
  • @mickl but isn't the ISO format by default a string? even the function to convert from Date object (which is type object) will result in a string. Just played around with the types and it is indeed a string. – SebastianG Apr 24 '19 at 18:23
  • Try to take a look here: https://mongoplayground.net/p/yLlWFxwAjA_ this is why I'm wondering if you're passing ISODate or string as a parameter – mickl Apr 24 '19 at 19:10
  • 1
    @mickl Your mention about the string got me pointed in the right direction and I manage to get it all working, I will update my post with the solution tomorrow but long-story short, mongoose already does the conversion to ISODate("string date stuff") and I only needed to pass an actual Date object. So I just declared const from = new Date(body.from); and passed from. – SebastianG Apr 24 '19 at 19:13

0 Answers0