MONGODB:查询结果不一致(损坏的数据库?)

时间:2014-09-17 14:16:27

标签: mongodb

我们的应用程序现在运行平稳约2年(至少在以下方面)。 但几周前,我们的一些客户开始抱怨系统中的相关数量。我们对它进行了调查,将这个例子简化为一个最小的案例,结果出现了这种奇怪的情况:

db.my_collection.find().count()
=> 250088

db.my_collection.distinct("field_1")
=> [false, true]

db.my_collection.find({"field_1":{$nin:[true, false]}}).count()
=> 0
db.my_collection.find({"field_1":{$in:[true, false]}}).count()
=> 250088


db.my_collection.find({"field_1":true}).count()
=> 140357
db.my_collection.find({"field_1":false}).count()
=> 109731
(140357 + 109731 = 250088, great!)


db.my_collection.find({"field_2":null}).count()
=> 4638
db.my_collection.find({"field_2":{$ne:null}}).count()
=> 245450
(4638 + 245450 = 250088, great!)


db.my_collection.find({"field_2":{$ne:null}, "field_1":{$in:[true, false]}}).count()
=> 245450 (great!)

但是看看这个:

db.my_collection.find({"field_2":null, "field_1":false}).count()
=> 2669
db.my_collection.find({"field_2":null, "field_1":true}).count()
=> 790
2669 + 790 = 3459 (should sum 4638 !!)

db.my_collection.find({"field_2":{$ne:null}, "field_1":false}).count()
=> 75795
db.my_collection.find({"field_2":{$ne:null}, "field_1":true}).count()
=> 107298
75795 + 107298 = 183093 (should sum 245450 !!)

另外1:只有在查询中使用此特定字段组合时才会出现此问题。如果我们将这两个字段中的每一个都与其他字段组合在一起,那么一切正常(我们在这里没有进行详尽的搜索。但我们尝试了一堆)

另外2:我们尝试使用各种不同的提示值(包括{$ natural:1})进行查询,以查看它是否可能是损坏的索引,但它仍然被破坏。

另外3:我们重新同步了我们的一个辅助部分,错误仍然存​​在。

另外4:我们通过控制台和find()。forEach({insert})查询,(相同的数据库版本)和IT工作正确地支持集合,文档,文档!

另外5:我们正在使用mongodb 2.4.8。

WTF可能正在发生? 我该怎么做才能纠正它? 我应该怎么做才能快速纠正?

=====更新18-sep

我们发现我们有一些索引的密钥太大而mongodb 2.4只记录并忽略它。所以索引正在默默地打破。 (看起来像mongo 2.6以更加精致的方式解决了这个问题:http://docs.mongodb.org/master//release-notes/2.6-compatibility/#index-key-length-incompatibility

我们删除了这些索引(它们只是我们制作并忘记的一些测试)并且查询回到了家。

我们只是没有意识到为什么在给出提示时我们无法得到正确的结果,即使是$ natural ...:...

0 个答案:

没有答案
相关问题