Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  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!

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading