mongodb:到期不起作用 [英] mongodb: expires not working

查看:138
本文介绍了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

有关更多详细信息,请在此处搜索过期": http://mongoosejs.com/docs/api .html

For more details search for "expires" here: http://mongoosejs.com/docs/api.html

这篇关于mongodb:到期不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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