mongoose – 推入物体的子arrays

我的mongoose对象:

{ "_id" : "568ad3db59b494d4284ac191", "name" : "MyCompany", "description": "whatever" "items" : [ { "_id" : "568ad3db59b494d4284ac19f", "fields" : { "Name" : "Item1", "Internal ID" : "ID00042", "tags" : [ { "Description" : "Tag1", "Level" : 2 }, { "Description" : "Tag2", "Level" : 3 } ] } }, { "_id" : "568ad3db59b494d4284ac19f", "fields" : { "Name" : "Item2", "Internal ID" : "ID00043", "tags" : [ { "Description" : "Tag1", "Level" : 5 }, { "Description" : "Tag5", "Level" : 1 } ] } }, {..} ] } 

我需要推下面的标签:

 var obj = { "Description" : "myDescription", "Level" : 3 }; 

进入以下项目的标签数组:

 var internal_id = "ID00102"; 

我的尝试不工作:

 Company.findOneAndUpdate( { "_id": "568ad3db59b494d4284ac191", "items.fields['Internal ID]": internal_id }, { "$push": { "tags": thetag } }, function(err,doc) { if (err) res.status(500).send(err); return res.status(200).send(doc); } ); 

$push操作符与更新中的$位置操作符一起应用,以将标记对象添加到tags字段。 $ position操作符将识别items数组中正确的元素进行更新,而不显式指定数组中元素的位置,因此最终的更新语句应该如下所示:

 Company.update( { "_id": "568ad3db59b494d4284ac191", "items.fields.Internal ID": internal_id }, { "$push": { "items.$.fields.tags": thetag } } )