Mad*_*ily 6 python bulkinsert mongodb pymongo
我正在尝试插入以下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'},
....
Run Code Online (Sandbox Code Playgroud)
我使用的代码:
collection = db.posts
collection.insert_many(p for p in posts )
Run Code Online (Sandbox Code Playgroud)
但是后来我收到一个错误,说BulkWriteError: batch op errors occurred 只能导入第一个字典(对应于第一个#AUTHID)
我找到了一个描述类似情况的链接,但它没有解释为什么会发生这种情况或如何解决这个问题。它在_Why do PyMongo为我的所有文档添加一个id 字段下?在以下链接中:https :
//github.com/mongodb/mongo-python-driver/blob/master/doc/faq.rst#id25
这是 此输出记录中的输出,这些记录存储在列表中。
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 )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7702 次 |
| 最近记录: |