【发布时间】:2017-01-09 19:09:15
【问题描述】:
我想计算存储在 mongodb bson 文件中的文档数量,而无需通过 mongo restore 将文件导入数据库。
我能想到的最好的python是
bson_doc = open('./archive.bson','rb')
it = bson.decode_file_iter(bson_doc)
total = sum(1 for _ in it)
print(total)
这在理论上可行,但当 bson 文档很大时,在实践中会很慢。谁有更快的方法来计算 bson 文档中的文档数量而无需进行完整解码?
我目前正在使用 python 2.7 和 pymongo。 https://api.mongodb.com/python/current/api/bson/index.html
【问题讨论】:
-
你使用的是哪个 bson 包?这是 mongodb bson 还是其他的?
-
使用此信息更新了问题。我使用的bson包如下api.mongodb.com/python/current/api/bson/index.html