Py学习  »  Irfan  »  全部回复
回复总数  1
5 年前
回复了 Irfan 创建的主题 » Mongodb更新子文档字段的方法比逐个更新更好[重复]

在mongo db中更新多个文档中的数组字段。

使用$pull或$push和update many查询更新mongoDb中的数组元素。

Notification.updateMany(
    { "_id": { $in: req.body.notificationIds } },
    {
        $pull: { "receiversId": req.body.userId }
    }, function (err) {
        if (err) {
            res.status(500).json({ "msg": err });
        } else {
            res.status(200).json({
                "msg": "Notification Deleted Successfully."
            });
        }
    });