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.