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

How to get dates in specific array mongo

I have mongo collection with objects with 2 fields: available_from and available_to

Mongo collection:

[
  {
    "_id": ObjectId("66ed4d5ede0184e906f9397a"),
    "available_from": ISODate("2024-09-20T00:00:00.000Z"),
    "available_to": ISODate("2024-09-30T00:00:00.000Z")
  }
]

I want to get all dates which are in specific array

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

For example my input is: {$match: {"available_from": "2024-09-20", "available_to": "2024-09-30"}} but this input still does not show me anything.

There is mongo playground:

What should i change?

>Solution :

  • Your query missed the time component, causing an exact match failure.
  • Please include time range along with the date.
  • Updated query is as follows:
db.collection.aggregate({
  $match: {
    "available_from": {
      $gte: ISODate("2024-09-20T00:00:00.000Z")
    },
    "available_to": {
      $lte: ISODate("2024-09-30T23:59:59.999Z")
    }
  }
})
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