MongoDB
新手,我正在尝试优化对数据库的批量写入。不过,我不明白如何初始化 Bulk()
操作。
我的理解是,由于插入将在集合上完成,这就是应该初始化 initializeUnorderedBulkOp()
的位置(或者更确切地说是“在什么”上):
下面的代码涵盖了我能想到的所有情况:
import pymongo
conn = pymongo.MongoClient('mongodb.example.com', 27017)
db = conn.test
coll = db.testing
# tried all of the following
# this one does not make much sense to me as I insert to a collection, added for completeness
bulk = db.initializeUnorderedBulkOp()
# that one seems to be the most reasonable to me
bulk = coll.initializeUnorderedBulkOp()
# that one is from http://blog.mongodb.org/post/84922794768/mongodbs-new-bulk-api
bulk = db.collection('testing').initializeUnorderedBulkOp()
# the staging and execution
# bulk.find({'name': 'hello'}).update({'name': 'hello', 'who': 'world'})
# bulk.execute()
引发的异常是
TypeError: 'Collection' object is not callable. If you meant to call the 'initializeUnorderedBulkOp' method on a 'Database' object it is failing because no such method exists.
第一种情况是'Database'
,后两种情况是'Collection'
我应该如何使用 initializeUnorderedBulkOp()
?
最佳答案
在 Python 中(使用 pymongo
模块),方法名称不是 initializeUnorderedBulkOp
而是 initialize_unordered_bulk_op
。
正如你猜对的那样,你必须在集合上调用它(在你的情况下,coll.initialize_unordered_bulk_op()
应该可以工作)。
关于python - 如何初始化UnorderedBulkOp()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27379598/