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

Time:02-06

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?

Thanks

CodePudding user response:

Simple $or will do the job:

(price cannot be <400 & >800 at the same time , but can be <400 or >800 at the same time)

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

playground

  •  Tags:  
  • Related