2016-08-17 1 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" 
      }] 
     } 
    } 
}) 
関連する問題