2016-08-03 4 views
2
[{ 
    "_id" : ObjectId("579de5ad16944ccc24d5f4f1"), 
    "dots" : 
    [ 
     { 
      "id" : 1, 
      "location" : 
      [ 
       { 
        "lx" : 10, 
        "ly" : 10 
       } 
      ] 
     }, 
     { 
      "id" : 2, 
      "location" : [{}] 
     } 
    ] 
}] 

上記はjson形式のモデルです(mongobooterから) "lines"といい、_idとdots.idがあり、新しいオブジェクトを追加したいと思います場所へ。どうすればいいですか(マングースを使って)?新しいオブジェクトをマングースのサブドキュメント配列フィールドに挿入します

+0

場所を?ドット配列? – devonJS

+0

"location"ドット配列オブジェクト..... – ASD

答えて

4

あなたが間に選択することができます。

Mongoseオブジェクトウェイ:($push$ operatorを使用して)

document.dots[0].location.push({ /* your subdoc*/ }); 
document.save(callback); 

モンゴ/マングース問合せ:

YourModel.update(
    {_id: /* doc id */, 'dots.id': /* subdoc id */ }, 
    {$push: {'dots.$.location': { /* your subdoc */ }}, 
    callback 
); 
+0

ありがとう... – ASD

+0

どちらが優れたパフォーマンスを提供しますか? – user1790300

関連する問題