Mongoose 过期属性无法正常工作 [英] Mongoose expires property not working properly

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

问题描述

答案

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

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

获取索引

使用以下示例中的代码确定,调用后:<代码>db.sampletexts.getIndexes()我得到

<预><代码>[{v":1,钥匙" : {_id":1},"name": "_id_",ns":test.sampletexts"},{v":1,钥匙" : {创建于":1},"name" : "createdAt_1","ns" : "test.sampletexts",expireAfterSeconds":5400,背景":真实,安全":空}]

所以虽然我指定了 60 秒,但它设置为 5400?嗯.

第二次更新

这是我尝试运行的最小示例.我一定在这里遗漏了一些东西,因为我实际上将我的模型从我的原始项目复制到了示例中(减去了一些额外的字符串字段),现在文档根本没有过期?请让我觉得自己很愚蠢,并意识到我遗漏了一些明显的东西.我想了想设置一个时间间隔来运行文档和当前时间之间的时差检查,然后意识到运行这么多通常会​​扩展的查询是多么糟糕.嗯...

MongoDB v3.0.1 &&猫鼬 v4.0.1

相关代码在:/routes/index.js

<小时>

我运行的一些测试值:

整个字段看起来像这样

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

测试 1:(测试 3 次)<代码>过期:'300s'

过期时间:2:00 &&2:50 &&2:28

测试 2:(测试 2 次)<代码>过期:'600s'

过期时间:2:08&&2:58

测试 3:<代码>过期:600过期时间:2:55

测试 4:<代码>到期:3600过期时间:2:13

更新:

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

<小时>

原始问题:

好的,我已经查看了其他一些关于使用 Mongoose 和 MongoDB 使文档过期的答案,但它们似乎是因为

a) 文档没有被删除

b) 它在被指定为

后被删除

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

这是我的模型:

var schema = mongoose.Schema({user_id:字符串,消息:字符串,createdAt: { type: Date, expires: '4h', default: Date.now },地点:对象});

版本:

猫鼬 4.0.1

mongodb 3.0.1(也在 2.6.7 中测试)

解决方案

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

db.sampletexts.dropIndex('createdAt_1')

the answer

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; - stop the app and delete the TTL index of the field you want to change (see comments / answer below)

getting indexes

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

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

second update

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 && Mongoose v4.0.1

relevant code in: /routes/index.js


Some test values I've run:

with the full field looking like this

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

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

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

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

expired at: 2:08 && 2:58

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

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

Update:

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?


Original Question:

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) the document isn't getting deleted

or

b) its getting deleted at a point way after it was designated to

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.

heres my model:

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

Versions:

mongoose 4.0.1

mongodb 3.0.1 (also tested in 2.6.7)

解决方案

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')

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

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