2012-03-21 4 views
2

파이썬/PyMongo와 같은 코드가 있습니다 (내부 조인을 시뮬레이트하고 두 번째 컬렉션의 모든 문서를 목록에 중첩하고 Mongo에서 첫 번째 적절한 문서에 추가해야 함). 아이디어는 두 개의 콜렉션을로드하고 두 개의 필드 (연결 필드 이름)와 같은 속성을 가진 두 번째 콜렉션의 모든 문서를 처음으로 나열하고 첫 번째 콜렉션에서 적절한 문서에 추가하는 것과 같습니다.PyMongo에서 문서를 업데이트 할 수 없습니다.

예를 들어, "국가", "인구"및 "국가"와 "자동차 공장"이있는 두 번째 문서는 모든 공장의 첫 번째 수집 목록에 넣고 싶습니다. 내가 가진

for f in first_collection_records: 
     temp=[] 
     id=f['_id'] 
     second_collection_records.rewind() 
     for s in second_collection_records: 
      if f[field_one]==s[field_two]: 
       temp.append(s) 
     f[self.__second_collection_name__]=temp 
     first_collection_records.update({"_id":id}, f, safe=True) 

) 해당 국가뿐만 오류 '커서'개체가 어떤 속성 '갱신'이 없습니다. 내가 뭘 잘못 했니?

+0

당신이 달성하고자하는 사항에 대한 자세한 특정 될 수 있을까? 어떤 종류의 것을 업데이트 하시겠습니까? .update ({ "_ id": id}, f, safe = True)는 무엇을 의미합니까? – EwyynTomato

+0

@EwyynTomato 새로운 코멘트가 추가되었습니다. 지금 제발 도와주세요. – Damir

답변

5

first_collection_recordspymongo.cursor.Cursor 개체입니다. db.first_collection.find()으로 전화하면 얻을 수있는 결과입니다. 당신은 원래 collection 객체에 update를 호출해야합니다

# assuming your query looked something like this 
first_collection_records = db.first_collection.find() 

# your code here.... 

# your last line should reference the collection object to do the update 
db.first_collection.update({"_id":id}, f, safe=True) 
관련 문제