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: find between two dates doesn't work

i’m trying to find between two dates. I have seen other similar questions on Stackoverflow.

My query should return 0 documents but i don’t know why it is returning the document.

field:

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

"dates": [
    {
      "$date": "2023-04-08T14:37:02.937Z"
    },
    {
      "$date": "2023-04-15T14:37:02.937Z"
    },
    {
      "$date": "2023-04-23T14:37:02.937Z"
    }
  ],

My query :

{ dates: {$gte:ISODate('2023-04-18'), $lt: ISODate('2023-04-20') } }

This query should return 0 documents but instead it returns the document.

>Solution :

Try using $elemMatch:

db.collection.find({
  dates: {
    $elemMatch: {
      $gte: ISODate("2023-04-18"),
      $lt: ISODate("2023-04-20")
    }
  }
})

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