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

Want to write mongodb query that all the numbers that are not between particular range

Below query i have written to get range between 400 to 800

db.products.find({$and: [{price: {$gt: 400}}, {price: {$lt: 800}}]});

But how to get values that are not between range of 400 to 800

db.products.find({$and: [{price: {$lt: 400}}, {price: {$gt: 800}}]});

why above query not shown any result?

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

Thanks

>Solution :

Simple $or will do the job:

(price cannot be <400 & >800 at the same time)

db.collection.find({
  $or: [
    {
      "price": {
        $lt: 400
      }
    },
    {
      "price": {
        $gt: 800
      }
    }
  ]
})

playground

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