【发布时间】:2015-02-02 20:21:11
【问题描述】:
我有一个使用 mongo 作为数据存储的 rails 应用程序。 Mongo 配置了 3 个查询路由器和 16 个分片。当我做类似的事情时:
count = Item.where(:confirmed => true).count
我得到了大约 80 万件物品。但是,当我运行批处理以实际遍历项目时,计数要小得多:
batch_size = 10000
offset_count = 0
completed_count = 0
# prime the pump
q = Item.where(:confirmed => true).limit(batch_size).skip(offset_count * batch_size).to_a
while q.count > 0
# do something
completed_count += q.count
offset_count += 1
q = Item.where(:confirmed => true).limit(batch_size).skip(offset_count * batch_size).to_a
end
# here, completed count is << count (where "count" is the initial .where count)
知道这里发生了什么吗? mongo 是否估计总数而不是从索引中计算它?
FWIW,有一个关于 :confirmed 的项目的索引,我在运行它之前重新编制了索引以确保没有索引损坏。
感谢您的帮助。 凯文
【问题讨论】:
-
感谢回复,我尝试添加排序,没有区别,仍然只得到第一个查询中返回的大约一半的项目数。
标签: ruby-on-rails mongodb mongoid