0

I am trying to insert a list of dict to a collection in MongoDB. I can correctly insert a specified dict using:

all_results_id = all_results.insert_one(json_data[0]).inserted_id

When trying to insert each of the dictionaries contained with list json_data using:

all_results_id = all_results.insert_many(json_data).inserted_id

I get this error:

  File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\pymongo\collection.py", line 753, in insert_many
    blk.execute(write_concern, session=session)
  File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\pymongo\bulk.py", line 521, in execute
    return self.execute_command(generator, write_concern, session)
  File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\pymongo\bulk.py", line 349, in execute_command
    _raise_bulk_write_error(full_result)
  File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\pymongo\bulk.py", line 140, in _raise_bulk_write_error
    raise BulkWriteError(full_result)
pymongo.errors.BulkWriteError: batch op errors occurred

In other questions related to this, people have mentioned that this is caused by a duplicated _id however the MongoDB documentation states:

"If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field."

Can anyone advise where I'm going wrong? I'd rather not iterate over the list and call insert_one on each.

WSC
  • 903
  • 10
  • 30
  • 1
    The "other answers" are basically right, in that your code completely ignores checking for errors when you really should be doing that. The listing in the linked question and answer shows checking the error response for exactly what type of errors those are. Also you cannot access a single `inserted_id` from an `insert_many()` since there are of course **"many"** values returned. Those are in the `result` object when there are no errors or part of the `error` response in the event an error was thrown. – Neil Lunn Apr 07 '19 at 12:31
  • See also [`BulkWriteResult`](https://api.mongodb.com/python/current/api/pymongo/results.html#pymongo.results.BulkWriteResult) and the [`errors`](https://api.mongodb.com/python/current/api/pymongo/errors.html) documentation for pymongo. It does not really give you any real information on `BulkWriteError`, but it's basically identical to the `BulkWriteResult`, just with a couple of error lists added. – Neil Lunn Apr 07 '19 at 12:37
  • Thanks. I'm still a bit confused as to why it's throwing an error at all, given that it should be creating a unique ObjectID. However, using try/except does seem to fix the issue. – WSC Apr 07 '19 at 13:16
  • Instead of being confused, you should be reading the content you have been linked to, implementing that in your own code or trying the sample listing and **inspecting the errors** in order to understand it. Chances are that the errors you are seeing are in fact "duplicate key errors", but there is little point in speculating when you now have a reference to use in inspecting the actual errors in order to see what they really say. Bottom line is **always inspect the error response**, which is what the linked answer basically says. It's just that "some errors" you can safely ignore when expected – Neil Lunn Apr 07 '19 at 13:25

0 Answers0