Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pymongo bulk inserts not working

I am following the tutorial http://api.mongodb.org/python/current/tutorial.html for bulk inserts. However, I am getting the error that I have listed below. What am I missing? The reviews_array is a json_array

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

db_handle = db.reviews


self.db_handle.insert_many(reviews_array)

The Error:

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.
like image 503
sheetal_158 Avatar asked Mar 13 '15 21:03

sheetal_158


2 Answers

In pymongo, before V3.0, you use insert for both single-doc and bulk insert. If you pass a document, it performs a single-insert, and if you pass a different iterable (list, generator), it performs bulk insert.

insert_many() does not exist in pymongo before V3.0.

In pymongo V3.0, they introduced insert_many and insert_one, and the use of the plain insert is deprecated.

like image 71
shx2 Avatar answered Nov 12 '22 20:11

shx2


I also got this error. While shx2's answer is right here, i got the error because i was using mongoengine and tried insert_many on the database, and it is a collection method, so it makes sense to do conn[database][collection].insert_many(iterable) and not conn[database].insert_many(iterable).

like image 32
negfrequency Avatar answered Nov 12 '22 20:11

negfrequency