Remove sub-document from Mongo with mongoose

Subdocuments now have a remove function. Use as follows from the spec:

var doc = parent.children.id(id).remove();
parent.save(function (err) {
  if (err) return handleError(err);
  console.log('the sub-doc was removed')
});

You want inventory.items.pull(req.params.itemSku), followed by an inventory.save call. .remove is for top-level documents


Removing a subdocument from an array

The nicest and most complete solution I have found that both finds and removes a subdocument from an array is using Mongoose's $pull method:

Collection.findOneAndUpdate(
    { _id: yourCollectionId },
    { $pull: { subdocumentsArray: { _id: subdocumentId} } },
    { new: true },
    function(err) {
        if (err) { console.log(err) }
    }
)

The {new: true} ensures the updated version of the data is returned, rather than the old data.