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
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
}
})
In mongoDB,you can can filter best like this:
db.collection.find()
.or([
{$gte: 8000000}
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With