当使用具有相同值的 $lte 和 $gte 查询不返回任何值时,mongoDB 问题(或没有)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18593772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:24:13  来源:igfitidea点击:

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

mongodbfindrange

提问by user2743177

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-.

忘了提到 mongoDB 版本是 2.4.4-pre-。

回答by zero323

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 }