Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query where values are less than equal

Tags:

mongodb

nosql

I have two documents:

{
    "_id" : ObjectId("5a2c7a84921716da1700b905"),
    "min" : 9000000,
    "name" : "One"
}
{
    "_id" : ObjectId("5a2b7848921716da1700b903"),
    "min" : 1400000,
    "name" : "Two"
}

So I want find only when the value of property min is less than equal to -for example-8000000. With mongo I try with

db.collection.find({
    "min" : {
        $lte : 8000000
    }
})

But this returns two documents and no only one like in SQL:

SELECT * FROM collection WHERE 8000000 <= collection.min;

How I can solved it?

P.S: I want says 8000000

like image 552
Jorge Olaf Avatar asked Jan 03 '23 02:01

Jorge Olaf


2 Answers

Your logic is flawed. In your SQL example, your condition is:

WHERE 8000000 <= collection.min

Now, let's swap the compared fields so that they match the order of your MongoDB query keeping the logic identical:

WHERE collection.min >= 8000000

You will notice that this query is effectively saying that collection.min must be greater than or equal to 8000000 - not smaller than or equal...

So your MongoDB query just needs to use the right operator:

db.collection.find({
    "min" : {
        $gte : 8000000
    }
})
like image 193
dnickless Avatar answered Jan 10 '23 12:01

dnickless


In mongoDB,you can can filter best like this:

db.collection.find()
.or([
     {$gte: 8000000}
   ])
like image 27
Alazar-dev Avatar answered Jan 10 '23 10:01

Alazar-dev