MongoDB: $match on today's date + $in

I am trying to figure out (what should be..) a simple query. In my document, I have the field:

date_array : {15 : 2023-01-03T00:00:00.000+00:00, 
              16: 2023-01-04T00:00:00.000+00:00,
              17 : 2023-01-05T00:00:00.000+00:00}

I am simply trying to query on {$in: todays_date}

For example:

  date_array : {$in : [new Date("<YYYY-mm-dd>")]},  #syntax error in the date function


  date_array : {$in : [new Date()]},  # includes the current time, which doesn't work

Any help is appreciated!

>Solution :

If you realy want to do it without a code, you can do:

  {$expr: [{$in: [{$dateTrunc: {date: "$$NOW", unit: "day"}}, "$date_array"]}]}

See how it works on the playground example

Leave a Reply