2016-06-07 3 views
3

mongo에서 중첩 된 json 구조를 업데이트하는 데 어려움이 있습니다. pongongo를 Mongoengine-Rest-framework과 함께 사용하고 있습니다.PyMongo : JSON 키가 mongo에서 업데이트되었습니다.

이 json은 동적 인 구조를 가지고 있으며 중첩되어 있기 때문에 mongo-engine ORM보다 pymongo를 사용하기로 결정했습니다.

작성, 검색 및 삭제 작업이 정상적으로 수행됩니다. 업데이트 문제에 대한 제안 사항이 있습니다.

는 몽고에 이미 존재하는 샘플 객체를 고려할 수 있습니다 :

st1 = { 
     "name": "Some_name", 
     "details": { 
        "address1": { 
         "house_no": "731", 
         "street": "Some_street", 
         "city": "some_city" 
          "state": "some_state" 
         } 
       } 
     } 

내가 _id를위한 조건 인 상태 업데이트 명령에 json으로 ST2를 전송하여 세부 사항 주소 2를 추가하여 ST1을 업데이트하려고하면 그 갱신,

st2 = { 
     "details": { 
        "address2": { 
         "house_no": "5102", 
         "street": "Some_street", 
         "city": "some_city" 
          "state": "some_state" 
         } 
       } 
     } 

나는, 몽고에서, 결과로 다음과 같은 개체 ST3를 얻을

st3 = { 
     "name": "Some_name", 
     "details": { 
        "address2": { 
         "house_no": " 5102", 
         "street": "Some_street", 
         "city": "some_city" 
          "state": "some_state" 
         } 
       } 
     } 

대신 st4 개체가 필요합니다.

st4 = { 
     "name": "Some_name", 
     "details": { 
        "address1": { 
         "house_no": "731", 
         "street": "Some_street", 
         "city": "some_city" 
          "state": "some_state" 
         }, 
        "address2": { 
         "house_no": "5102", 
         "street": "Some_street", 
         "city": "some_city" 
          "state": "some_state" 
         } 
       } 
     } 

내 업데이트 명령은 다음과 같습니다

result = collection.update_one({'_id': id}, doc) 

ID : 문서의 _id

문서 : (여기) ST2

모음 : pymongo의 colllection 객체

원래 JSON 깊이는 6이고 키는 동적입니다. 업데이트는 다른 깊이에서 필요합니다.

+0

가능한 중복 http://stackoverflow.com/questions/4372797/how-do-i-update-a-mongo-document-after-inserted-it) – jano

+0

다른 답변 : http://stackoverflow.com/questions/13710770/how-to-update-values-using -pymongo – jano

답변

1

첫째,이에 업데이트 할 객체를 변경 :이 주소 2를 추가 할 수

collection.update_one({_id: id}, { '$set': {"details.address2" : to_update} }); 
0

사용 : 당신이 원하는 문서의 특정 부분을 업데이트하는 데 사용할 다음

to_update = { 
    "house_no": "5102", 
    "street": "Some_street", 
    "city": "some_city", 
    "state": "some_state" 
} 

을 그리고 :

collection.update ({ '_ ID': ObjectId가 (DOC_ID)}, { '$ 세트': '. 자세한 내용 %의'{% '주소 2'요지 (S2)}} upsert = 참)

체크 아웃 전체 코드 :

import pymongo 
from bson.objectid import ObjectId 

data = {"name": "Some_name", 
     "details": {"address1": {"house_no": "731", "street": "Some_street", "city": "some_city", "state": "some_state"}}} 

address2 = {"house_no": "731", "street": "Some_street", "city": "some_city", "state": "some_state"} 

connect = pymongo.MongoClient('192.168.4.202', 20020) 
database = connect['my_test'] 
collection = database['coll'] 

# # CREATE COLLECTIONS AND INSERT DATA 
# _id = collection.insert(data) 
# print _id 

doc_id = '57568aa11ec52522343ee695' 

collection.update({'_id': ObjectId(doc_id)}, {'$set': {'details.%s' % 'address2': address2}}, upsert=True) 
[? 내가 그것을 삽입 후 몽고 문서를 업데이트하려면 어떻게]의 (
+0

키가 동적이고 다른 깊이에있을 때는 작동하지 않습니다. 나는 그 질문을 갱신했다. – Shipra

+0

@Shipra - 키는 "address2"대신 동적 일 수 있습니다. 변수는 키 이름을 사용합니다.'details. % s. % s. % s'% ('var1', 'var2', 'var3')를 사용하는 것보다 더 깊은 곳으로 가고 싶다면. –