How do you remove a subdoc named task? The parent schema is User and the child schema is named Task.
Here is my route:
app.delete('/api/tasks/:id', isAuthenticated, function (req, res) {
User.update({ 'task._id': req.params.id }, { $pull: { 'task.$.id': req.params.id }},
(function(err, user) {
if(!err) {
console.log("Deleted Task" ),
res.redirect('/home');
}
})
);
});
And a bit of ajax:
// Delete
$(document).ready(function() {
$('.task-delete').click(function(event) {
$target = $(event.target)
$.ajax({
type: 'DELETE',
url: apiDeleteTask + $target.attr('data-task-id'),
success: function(response) {
$target.parent.children.id(id).remove();
$alert.trigger('success', 'Task was removed.');
},
error: function(error) {
$alert.trigger('error', error);
}
})
});
})
Why is this not working?
The $pull operator does not work here because it only works on arrays and 'task._id' is not an array, but rather a field within an array.
Read more here: http://docs.mongodb.org/manual/reference/operator/update/pull/#up._S_pull