I have a circuit that looks something like this:
var postSchema = new Schema({ created: { type: Date, default: Date.now }, updated: { type: Date, default: Date.now }, comments: { type: [Schema.ObjectId], ref: 'Comment' } });
So my comment collection is a collection of object identifier, referencing my comment scheme / comment collection.
I need to remove some of them on request, so I'm trying to do this:
var comments = [1, 2, 4]; Post.update({ _id: post_id}, {'$pullAll': {comments: comments }}) .exec(function(err) {
After executing the above code, I deleted the comment identifiers from Post.comments, but I also need to remove these comments from the Comments collection. How can I do it?
EDIT:. How to get the identifiers of documents that were not actually deleted. A simple example:
Post.comments = [1, 2, 3]; Post.update({ _id: post_id}, {'$pullAll': {comments: [1,2]}});
In the above code, Post.com has only 1,2,3, but we are trying to pull out [1,2], so I need to know that id = 3 does not exist on Post.comments, and I donβt need to remove it from the collection ' Comments'.