我正在尝试将以下名为posts
的词典列表插入到mongo中,并出现BulkWriteError: batch op errors occurred
错误,我不知道如何修复。
posts:
[{'#AUTHID': 'fffafe151f07a30a0ede2038a897b680',
'Records': [
{'DATE': '07/22/09 05:54 PM',
'STATUS': 'Is flying back friday night in time to move the rest of his stuff then go to work the next morning... great.'},
......
{'DATE': '07/19/09 04:39 PM', 'STATUS': 'is stealing his net by the lake'}]},
{'#AUTHID': 'fffafe151f07a30a0ede2038a897b680',
'Records': [
{'DATE': '07/22/09 05:54 PM',
'STATUS': 'Is flying back friday night in time to move the rest of his stuff then go to work the next morning... great.'},
{'DATE': '07/19/09 04:39 PM', 'STATUS': 'is stealing his net by the lake'},
....
我使用的代码:
collection = db.posts
collection.insert_many(p for p in posts )
但后来我收到一条错误BulkWriteError: batch op errors occurred
并且只设法导入第一个字典(对应第一个字典#AUTHID
)
我找到了一个描述类似情况的链接,但它没有解释为什么会发生这种情况或如何解决这个问题。它位于 _为什么PyMongo会在我的所有文档中添加 id字段? 在以下链接中:
https://github.com/mongodb/mongo-python-driver/blob/master/doc/faq.rst#id25
答案 0 :(得分:1)
在这里回答不迟,你几乎就在那里。我不确定FAQ是否已更新,但请正确阅读:
使用单个文档的引用列表调用
insert_many()
时会引发BulkWriteError
请注意,它表示单个或换句话说,相同的实例。 FAQ中的示例显示了如何使用相同的实例生成错误。您可以使用id()
来显示内存地址,以检查它是否相同。事实上,我可以看到你的文件内容是一样的。最有可能(但不一定)是同一个实例。
print id(posts[0])
print id(posts[1])
如果任何dict具有相同的实例,那么在准备posts
变量时出错了。只需确保所有列表项都有不同的实例,因为您要插入(许多)不同的文档!
答案 1 :(得分:0)
Here is output 在这个输出记录中是存储列表。
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client['post']
posts = [{'#AUTHID': 'fffafe151f07a30a0ede2038a897b680',
'Records': [
{'DATE': '07/22/09 05:54 PM',
'STATUS': 'Is flying back friday night in time to move the rest of his stuff then go to work the next morning... great.'},
{'DATE': '07/19/09 04:39 PM', 'STATUS': 'is stealing his net by the lake'}]},
{'#AUTHID': 'fffafe151f07a30a0ede2038a897b680',
'Records': [
{'DATE': '07/22/09 05:54 PM',
'STATUS': 'Is flying back friday night in time to move the rest of his stuff then go to work the next morning... great.'},
{'DATE': '07/19/09 04:39 PM', 'STATUS': 'is stealing his net by the lake'}]}]
collection = db.posti.insert_many(p for p in posts )