Monday, 15 May 2017

Node.js with mongoose delete one array element

guys.

I am trying to delete one array element when I click delete button on jade view page.

When clicked, it's going to send selected instructor objected as req.body.

At sever side, it will find courses that contain the instructor objectId.

Any idea for me?

Thank you for reading it.

here is my code:

var id = req.body._id;
clist.find({ instructors: { $in: [id] } }).exec(function (err, result) {
            result.forEach(function (obj) {
                clist.update(
                    { _id: new mongoose.Types.ObjectId(obj._id)},
                    { $pull: { instructors : [new mongoose.Types.ObjectId(id)] } }
                );
                console.log(new mongoose.Types.ObjectId(obj._id) + ' was deleted');
            });
        });



via Chi Seong Oh

No comments:

Post a Comment