如何在对象的嵌套数组中提取特定属性

时间:2019-06-04 12:41:46

标签: node.js mongodb

任何人都可以帮助您如何在对象的嵌套数组中提取特定属性。

article:Array
 0:Object
articleId:"WMCA675107"
content:Array
0:Object
title:"my new title"
content:"asasasa"
Randid:300979
illustrations:Array
0:Object
title:"sss"
imagePath:"1559647117293.JPG"
randid:14528
1:Object
title:"xyz"
imagePath:"54124841548.JPG"
randid:88985

我试图仅删除插图数组中的imagePath。 我这样尝试过

Collections.user.update(
        { "article.articleId": req.body.articleId} ,
        { $pull: { 'article.$[].content.$[].illustrations':  { "imagePath":undefined  } } } ,
        function(err,result) {
          if(err)
           console.log(err);
           else
           console.log(result);
             res.send("Deleted");
      })

它将删除对象的整个插图。我只需要删除一个属性,即插图数组中的imagePath。

请帮助任何人。

1 个答案:

答案 0 :(得分:0)

如果

$pull在对象内找到匹配的属性,它将删除整个对象。如果您只想删除该特定字段,则必须将其设置为undefined。像这样:

illustrations.imagePath = undefined

希望这会有所帮助。