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

以上是模型(從mongobooter)讓我們說「行」的JSON格式,我有_id和dots.id,我想添加新對象進入位置。那我該怎麼做(使用貓鼬)?插入一個新對象到子文件陣列場貓鼬

+0

哪個位置?點陣列? – devonJS

+0

「位置」與點陣對象..... – ASD

回答

4

您可以選擇之間:

Mongose對象的方式:

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

蒙戈/貓鼬查詢(使用$push$ operator):

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

非常感謝... – ASD

+0

哪一個提供更好的性能? – user1790300