我怎么知道一个带有过期属性的文档被删除了? [英] How Do I Know A Document With Expires Attribute Got Deleted?

查看:35
本文介绍了我怎么知道一个带有过期属性的文档被删除了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Node.js 中实现一些验证程序.我通过 Mongoose 将每个验证存储在我的 MongoDB 中,并为其设置了 expires 属性,因此一段时间后它将被删除.像这样

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,
  },
});

但我想知道该文档何时被删除.所以我可以做其他事情,比如删除与该验证相关的文档.我试过使用 post() 钩子,

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
};

但这似乎行不通,因为它是在应用程序级别.文档被MongoDB直接删除了,所以remove()不会被调用.

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.

推荐答案

您无法知道文档何时会被删除,因为 MongoDB 在后台任务中删除了过期文档.目前无法查看哪些文档被删除.

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.

如果您确实需要此功能,您可以创建一个后台作业,每 60 秒从您自己的集合中删除文档,然后通知您哪些文档已被删除.

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.

这篇关于我怎么知道一个带有过期属性的文档被删除了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆