2016-08-03 55 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