猫鼬过期财产无法正常工作 [英] Mongoose expires property not working properly

查看:91
本文介绍了猫鼬过期财产无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

答案

原来,当我在Mongoose中使用 expires 属性进行测试时,我成功在数据库中设置了TTL索引,但没有意识到当我更改Mongoose模式中的时间时我需要先从数据库中删除以前的TTL索引.

So it turns out when I was testing using the expires property in Mongoose, I successfully set a TTL index in the database, but didn't realize that when I changed the time in my mongoose schema I'd need to delete the previous TTL index out of the database first.

TLDR; -停止应用并删除要更改的字段的TTL索引(请参见下面的评论/答案)

TLDR; - stop the app and delete the TTL index of the field you want to change (see comments / answer below)

获取索引

在调用后,使用下面示例中的代码确定: db.sampletexts.getIndexes() 我知道了

Ok with the code from the below example, after calling: db.sampletexts.getIndexes() I get

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test.sampletexts"
    },
    { 
        "v" : 1,
        "key" : {
            "createdAt" : 1
        },
        "name" : "createdAt_1",
        "ns" : "test.sampletexts",
        "expireAfterSeconds" : 5400,
        "background" : true,
        "safe" : null
    }
]

所以尽管我指定60秒,但将其设置为5400?嗯.

so although I'm specifying 60 seconds, its setting to 5400? huh.

第二次更新

这里是我尝试运行的示例的最小示例.我必须在这里丢失一些东西,因为我实际上是将模型从原始项目复制到示例中(减去了几个额外的字符串字段),现在文档还没有过期吗?拜托,让我感到愚蠢,意识到我缺少明显的东西.我想了一秒钟来设置一个间隔来运行文档和当前时间之间的时差检查,然后意识到运行这么多经常扩展的查询有多糟糕.嗯...

Heres a minimal example of what I'm trying to run. I must be missing something here because I actually copied my model from my original project to the example (minus a few extra string fields), and now the document doesn't expire at all? Please, make me feel dumb and realize I'm missing something obvious. I thought for a second to set an interval to run the time difference check between the docs and the current time, then realized how badly running that many queries that often would scale. Hmm...

MongoDB v3.0.1&&猫鼬v4.0.1

MongoDB v3.0.1 && Mongoose v4.0.1

/routes/index.js中的相关代码

relevant code in: /routes/index.js

我已经运行过一些测试值:

Some test values I've run:

整个字段看起来像这样

createdAt: { type: Date, expires: 3600, default: Date.now }

测试1 :(测试3次) expires: ’300s’

test 1: (tested 3x) expires: ’300s’

已过期: 2:00&& 2:50&& 2:28

expired at: 2:00 && 2:50 && 2:28

测试2 :(测试2次) expires: ‘600s'

test 2: (tested 2x) expires: ‘600s'

已过期: 2:08 && 2:58

expired at: 2:08 && 2:58

测试3 : expires: 600 到期时间:2:55

test 3: expires: 600 expired at: 2:55

测试4 : expires: 3600 到期时间: 2:13

test 4: expires: 3600 expired at: 2:13

更新:

我又回来了,并决定,因为我尝试了不同版本的MongoDB,所以我尝试了不同版本的Mongoose v3.7.4 .不幸的是,我得到了相同的结果.我开始怀疑这是否可能是我的实际计算机(我在本地主机上运行)引起的问题?我不确定MongoDB如何获取/设置/监视其DateTime,但是我假设它必须从我的计算机设置中获取它.我有什么好的方法可以进一步测试/调试吗?

I'm back at it and decided since I tried a different version of MongoDB, I'd try a different version of Mongoose v3.7.4. Unfortunately I got the same result. I'm beginning to wonder if this could be an issue caused by my actual computer (I'm running on localhost)? I'm not sure how MongoDB gets/sets/monitors its DateTime, but I'm assuming that it must be getting it from my computers settings. Is there a good way I can go about testing/debugging this further?

原始问题:

好吧,我还查看了有关Mongoose和MongoDB在此处过期的文档的其他一些答案,但这似乎是因为

Ok I've looked at a few other answers regarding expiring docs here with Mongoose and MongoDB, but they seem to be because either

a)文档未被删除

b)将其指定为

我的问题是该文档将被删除,但是1分钟后无论如何都将被删除.因此,当我指定时间时,无论何时,它都会在60秒后将其删除.我看到MongoDB每分钟都会运行一次检查,以删除指定的文档,并且默认情况下为60秒,因此我假设MongoDB确认了可到期文档,但没有传递TTL参数,这让我很困惑.任何帮助将不胜感激.

My issue is that the document is getting deleted but after 1 minute no matter what. So when I specify a time, doesn't matter when, it deletes it after 60 seconds. I see that MongoDB runs a check every minute that removes specified docs, and its defaulted at 60 seconds, so I'm assuming that MongoDB is acknowledging the expire-able document, but not getting the TTL parameter passed, and I'm pretty stumped. Any help would be appreciated.

这是我的模型:

var schema = mongoose.Schema({
    user_id: String,
    message: String,
    createdAt: { type: Date, expires: '4h', default: Date.now },
    location: Object
});

版本:

猫鼬4.0.1

mongodb 3.0.1(也在2.6.7中进行了测试)

mongodb 3.0.1 (also tested in 2.6.7)

推荐答案

修改Mongoose模式不会修改现有索引,因此您需要手动删除TTL索引并重新启动应用程序,以使用当前的定义.

Modifying your Mongoose schema won't modify an existing index, so you need to manually drop the TTL index and restart your app to re-create the index using the current definition.

db.sampletexts.dropIndex('createdAt_1')

这篇关于猫鼬过期财产无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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