2016-08-17 2 views
-1

저는 PyMongo를 사용하고 있으며이 문서의 예제와 같은 객체 배열 인 필드를 가진 콜렉션에이 문서를 가지고 있습니다끝에있는 요소를 추가하는 컬렉션의 임베디드 문서 업데이트

{ 
    "_id" : ObjectId("509df76fbcf1bf5b27b4a23e"), 
    "field1" : "asfasfdas", 
    "field2" : "asfasdfa", 
    "embedded" : [ 
     { "field1" : "asdfasdf", "field2" : "asdfasdfa" }, 
     { "field1" : "asdfasfth.", "field2" : "asdfasf" } 
    ] 
} 

그래서 embedded 필드에 새 개체를 추가하고 싶습니다. 이를 달성하기 위해 어떤 방법을 사용할 수 있습니까?

+0

['update_one'] (http://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection. update_one) 또는 ['update_many'] (http://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.update_many)를 클릭하십시오. 또한 ['$ push'] (https://docs.mongodb.com/manual/reference/operator/update/push/) 연산자가 필요합니다 – styvane

+0

예를 들어 주시겠습니까? – provola

답변

1

사용 업데이트 쿼리 및 $ 밀어

db.collection.update({ 
    _id: "509df76fbcf1bf5b27b4a23e" 
}, { 
    $push: { 
     embedded: { 
      $each: [{ 
       "field1": "test1", 
       "field2": "test1" 
      } { 
       "field1": "test2", 
       "field2": "test2" 
      }] 
     } 
    } 
})