gpt4 book ai didi

python - mongodb的cursor.count()是实际计数吗?

转载 作者:行者123 更新时间:2023-12-01 05:45:45 25 4
gpt4 key购买 nike

当我扫描 mongodb 中的集合并编辑所有项目时,我得到了这个问题:扫描计数器 i 可以大于cursor.count()。 为什么会发生这种情况?有人能猜出来吗?

db.baiduwords.find().count() = 805280

db.baiduwords.find().size() = 805280

但是我达到了 1498400 或更多。

import pymongo
if __name__=='__main__':
client = pymongo.MongoClient()
i = 0
collection = client.baike.baiduwords.find()
for item in collection:
i += 1
MajorClass = []
for cl in item['C']:
c = cl
while(1):
a = client.baike.baiduclass.find({'s':c})
if a.count():
a = a[0]
if a['f'] == u'Root':
MajorClass.append(c)
break
else:
c = a['f']
else:
break
item['MC'] = list(set(MajorClass))
client.baike.baiduwords.save(item)
if i%100 == 0:
print "%d/%d"%(i, collection.count())

PS:printShardingStatus:此数据库未启用分片。

最佳答案

当您保存项目时,更新的文档可能会移动,导致光标多次返回该文档。

来自http://docs.mongodb.org/manual/faq/developers/#how-do-i-isolate-cursors-from-intervening-write-operations :

As a cursor returns documents, other operations may interleave with the query: if some of these operations are updates that cause the document to move (in the case of a table scan, caused by document growth,) or that change the indexed field on the index used by the query; then the cursor will return the same document more than once.

关于python - mongodb的cursor.count()是实际计数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16192668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com