user_dev
user_dev

Reputation: 1431

How can I delete items from multiple documents in mongodb with node.js?

I am trying to delete items from different documents in mongodb with one http.delete call. Here is the code I have written but it is just deleting item from the first document not from the other two documents. Here is the code I have written:

 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);
            });


        });
});

Here is my schema of other two documents:

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
});

Upvotes: 0

Views: 75

Answers (1)

AJS
AJS

Reputation: 2023

You have to add option multi : true to your query to tell mongo to update multiple documents like this:

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

});

Upvotes: 1

Related Questions