2016-07-13 4 views
2

posts이라는 사전 목록을 mongo에 삽입하려고 시도했지만 수정 방법을 모르는 BulkWriteError: batch op errors occurred 오류가 발생했습니다.Pymongo insert_many BulkWriteError

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

위의 샘플'posts'에 따르면 내용이 동일하기 때문에 문서가 복제 된 것 같습니다. ** 단일 문서 **를 여러 개로 삽입하는 것에 대한 FAQ가 나와 있습니다. 나는 당신의'dict (s)'이 같은 경우라고 생각합니다. – CallMeLaNN

답변

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) 
1

여기 거의 대답하지 않습니다. 나는 FAQ가 업데이트 한 경우 확실하지 오전하지만 제대로 읽어 보시기 바랍니다 :

단일 문서를 참조 목록에 insert_many()를 호출하면이 단일 또는 말한다

주 BulkWriteError을 제기

때 즉, 동일 인스턴스입니다. FAQ의 예제는 동일한 인스턴스로 오류를 생성하는 방법을 보여줍니다. 메모리 주소를 표시하려면 id()을 사용하여 동일한지 확인할 수 있습니다. 사실, 귀하의 문서 내용이 동일하다는 것을 알 수 있습니다. 아마 (반드시 그런 것은 아닙니다) 동일한 인스턴스입니다.

print id(posts[0]) 
print id(posts[1]) 

같은 인스턴스를 갖는 DICT의 다음 posts 변수를 준비하는 동안 뭔가 잘못. 다른 문서를 (많은) 삽입하기 때문에 모든 목록 항목이 다른 인스턴스를 가져야합니다.