本文介绍了mongodb:到期不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在猫鼬5.0.6中,我希望此架构在创建后1分钟内使文档过期:
In mongoose 5.0.6, I'm hoping this schema to expire documents 1min after creation:
const InvitationTokenSchema = new Schema( { token: { type: String, required: true }, createdAt: { type: Date, default: Date.now, expires: '1m' }, userId: { type: Schema.Types.ObjectId, ref: 'User' }, }, { usePushEach: true, }, );但是它根本不起作用-所有文档仅在mongo中保留,不会被删除.
However it simply doesn't work - all documents just persists in mongo, not being removed.
在mongo shell中,getIndexes()显示以下内容:
In mongo shell, getIndexes() shows the following:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mydb.invitationtokens" }, { "v" : 2, "key" : { "createdAt" : 1 }, "name" : "createdAt_1", "ns" : "mydb.invitationtokens", "expireAfterSeconds" : 60, "background" : true } ]可能是什么原因?
推荐答案正如我看到的代码所示,它是正确的. 另外,该值应为 String ,并且您也可以使用'1m'. 您需要将猫鼬更新到最新版本.
As I see you code, its right. Also, the value should be String and you can use '1m' as well. You need to update the mongoose to the latest version.
使用:npm update mongoose
有关更多详细信息,请在此处搜索过期": mongoosejs/docs/api .html
For more details search for "expires" here: mongoosejs/docs/api.html
更多推荐
mongodb:到期不起作用
发布评论