ialmasi
ialmasi

Reputation: 119

mongoDB issue(or not) when using $lte and $gte with the same value the query doesn't return any value

Let me give you an example:

mongos> db.test.insert( { a: 0.1, b: 1, c : 0.01 } )
mongos> db.test.insert( { a: 0.2, b: 2, c : 0.02 } )
mongos> db.test.insert( { a: 0.3, b: 3, c : 0.03 } )
mongos> db.test.find()
{ "_id" : ObjectId("5225e072dd144e45bf406d42"), "a" : 0.1, "b" : 1, "c" : 0.01 }
{ "_id" : ObjectId("5225e07add144e45bf406d43"), "a" : 0.2, "b" : 2, "c" : 0.02 }
{ "_id" : ObjectId("5225e082dd144e45bf406d44"), "a" : 0.3, "b" : 3, "c" : 0.03 }
mongos> db.listings.find({a: { $gte: 0.2, $lte: 0.2 } } );
mongos> db.listings.count({a: { $gte: 0.2, $lte: 0.2 } } );
0

As you can see, even if the document exists in the collection, the query doesn't return any result. I know that this is wierd to use range search for a simple equal, but in my opinion this should work and return the expected document. Thank you for your answer.

Forgot to mention the mongoDB verison is 2.4.4-pre-.

Upvotes: 10

Views: 38533

Answers (1)

zero323
zero323

Reputation: 330073

Problem is you inserted data into test collection but perform query on listings collection. If you query test collection everything works as expected:

> db.test.insert( { a: 0.1, b: 1, c : 0.01 } )
> db.test.insert( { a: 0.2, b: 2, c : 0.02 } )
> db.test.insert( { a: 0.3, b: 3, c : 0.03 } )

> db.test.count({a: { $gte: 0.2, $lte: 0.2 } } )
1
> db.test.find({a: { $gte: 0.2, $lte: 0.2 } } );
{ "_id" : ObjectId("5225ecd155feffaf74591328"), "a" : 0.2, "b" : 2, "c" : 0.02 }

Upvotes: 25

Related Questions