Update multiple subdocument without exact sub document level condition

≯℡__Kan透↙ 提交于 2019-12-08 03:58:26

Per this issue New operator to update all matching items in an array, currently there is no operation to do that in mongodb. Feel so sad, this issue lasts for 6 years.

There could be one work around in mongo shell as below.

> db.comments
    .find({})
    .forEach(function(doc) { 
                        doc.comments.map(function(c) {
                                if (c.active == 1) {
                                    c.status = 0;
                                 }
                        }); 
                        db.comments.update(
                                      {_id: doc._id}, 
                                      {$set: {comments: doc.comments}});
     });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!