如何使用node.js从mongodb中的多个文档中删除项目?

时间:2016-04-20 12:04:25

标签: node.js mongodb

我正在尝试通过一次http.delete调用从mongodb中的不同文档中删除项目。这是我编写的代码,但它只是从第一个文档中删除项目,而不是从其他两个文档中删除。这是我写的代码:

 app.delete('/deleteActor/:productName/:actor', function(req, res) {


    console.log("Deleting Actor" + req.params.productName + ":" + req.params.actor);

        impactMapActor.update({productName:req.params.productName}, { $pull: { actor:req.params.actor}},function (err, data) {

            if (err)
            //{
                res.send(err);
            console.log(err);


            impactMapActivity.remove({productName:req.params.productName},{actor:req.params.actor},function (err, data) {

                impactMapFeature.remove({productName:req.params.productName},{actor:req.params.actor},function (err, data) {

                });
            });

            impactMapActor.find({
                productName : req.body.productName
            }, function(err, data) {
                if (err)
                    res.send(err);

                console.log("Printing Data" + data)
                res.json(data);
            });


        });
});

这是我的其他两个文件的架构:

module.exports = mongoose.model('ImpactMapActivity', {

    productName:String,
    actor: String,
    activity: [{ type:'String' }],
});
module.exports = mongoose.model('ImpactMapFeature', {

    productName: String,
    actor: String,
    activity: String,
    feature: String,
    issueKey: String
});

1 个答案:

答案 0 :(得分:1)

您必须在查询中添加选项multi:true以告诉mongo更新多个文档,如下所示:

impactMapActor.update({productName:req.params.productName}, { $pull: {actor:req.params.actor}},{multi:true},function (err, data) {

});