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이고 키는 동적입니다. 업데이트는 다른 깊이에서 필요합니다.
가능한 중복 http://stackoverflow.com/questions/4372797/how-do-i-update-a-mongo-document-after-inserted-it) – jano
다른 답변 : http://stackoverflow.com/questions/13710770/how-to-update-values-using -pymongo – jano