Pymongo散装插入不起作用

时间:2015-03-13 21:23:31

标签: mongodb collections mongodb-query pymongo bulkinsert

我正在关注批量插入的教程http://api.mongodb.org/python/current/tutorial.html。但是,我收到了下面列出的错误。我错过了什么? reviews_array是一个json_array

client = MongoClient()
client = MongoClient('localhost', 27017)
db = client.is_proj

db_handle = db.reviews


self.db_handle.insert_many(reviews_array)

错误:

TypeError: 'Collection' object is not callable. If you meant to call the 'insert_many' method on a 'Collection' object it is failing because no such method exists.

2 个答案:

答案 0 :(得分:8)

在pymongo中,在V3.0之前,您使用insert进行单文档和批量插入。如果传递文档,则执行单插入,如果传递不同的可迭代(列表,生成器),则执行批量插入。

在V3.0之前的pymongo中不存在

insert_many()

在pymongo V3.0中,他们引入了insert_manyinsert_one,并且不推荐使用普通insert

答案 1 :(得分:0)

我也遇到了这个错误。虽然shx2的答案就在这里,但由于我正在使用mongoengine并在数据库上尝试过insert_many,因此出现了错误,这是一种收集方法,因此可以使用conn [database] [collection] .insert_many(iterable)而不是conn [database] .insert_many(iterable)。