Home > Software design >  MongoDB: $match on today's date $in
MongoDB: $match on today's date $in

Time:01-06

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
}

or

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

Any help is appreciated!

CodePudding user response:

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

db.collection.find(
  {$expr: [{$in: [{$dateTrunc: {date: "$$NOW", unit: "day"}}, "$date_array"]}]}
)

See how it works on the playground example

  • Related