Reputation: 115
I'm implementing some verification procedures in Node.js. I had each verification stored in my MongoDB by Mongoose, and set a expires attribute for it, so it will be deleted after some time. Like this
var verification = new Schema({
// something else
createdAt: {
type: Date,
expires: '1d',
default: Date.now,
},
});
But I want to know when that doc is deleted. So I can do something else, like deleting the docs related with that verification. I've tried using post()
hooks,
verification.post('remove', function(){
// do something else
};
But it seems this won't work, since it's on application level. The doc is deleted by MongoDB directly, so remove()
won't be called.
Upvotes: 1
Views: 175
Reputation: 12240
You can't know when the document will be deleted because MongoDB removes the expired documents in a background task. There is currently no way to check which documents were deleted.
If you really need this functionality, you can create a background job that will delete documents from your own collections every 60 seconds and then notify you which documents were deleted.
Upvotes: 2