我试图将字典列表插入MongoDB中的集合。我可以使用以下命令正确插入指定的字典:
all_results_id = all_results.insert_one(json_data[0]).inserted_id
当尝试使用以下命令插入列表json_data包含的每个字典时:
all_results_id = all_results.insert_many(json_data).inserted_id
我收到此错误:
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
在与此相关的其他问题中,人们提到这是由重复的_id
引起的,但是MongoDB文档指出:
“如果插入的文档省略_id字段,则MongoDB驱动程序会自动为_id字段生成ObjectId。”
谁能告诉我我要去哪里错了?我不希望遍历该列表并在每个列表上调用insert_one
。