Remove an ObjectId from an array of objectId
I have a very similar setup with an "Event" object that has an array of "Assignment" objects saved as an array of ObjectIds. I was able to simply use
obj.arrayField.remove(idToRemove);
Here is the relevant code inside of my delete route handler:
var id = req.assignment._id;req.event.assignments.remove(id);req.event.save(function(err, event) { //etc }
Does this work for you?
unit.items.remove(request.params.itemId);
You need to pass the string into mongoose.Types.ObjectId('')
to get an actual object you can compare against.
So _.remove(unit.items, mongoose.Types.ObjectId(req.params.itemId));