在mongodb 2.6.7中使用TTL未到期的文档

时间:2015-02-18 14:38:59

标签: mongodb mongo-shell

我在mongo shell中尝试TTL功能,但我似乎无法使其正常工作。我使用documentation对所有内容进行三重检查。

这是我做的:

MongoDB shell version: 2.6.7
connecting to: test

> db.streamers.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.streamers"
        },
        {
                "v" : 1,
                "key" : {
                        "room" : 1
                },
                "name" : "room_1",
                "ns" : "test.streamers",
                "background" : true,
                "safe" : null
        },
        {
                "v" : 1,
                "key" : {
                        "lastAlive" : 1
                },
                "name" : "lastAlive_1",
                "ns" : "test.streamers",
                "expiresAfterSeconds" : 60,
                "background" : true,
                "safe" : null
        }
]

> db.streamers.insert({ _id: "hello", room: "yop", lastAlive: new Date() })
WriteResult({ "nInserted" : 1 })

[等一会儿......]

> db.streamers.find({ _id: "hello" })
{ "_id" : "hello", "room" : "yop", "lastAlive" : ISODate("2015-02-18T13:03:02.836Z") }

> new Date()
ISODate("2015-02-18T13:50:50.403Z")

很明显,即使等了一个多小时,文档也没有删除。 db.currentOp()也返回一个空数组。

这是一个开发环境,因此mongodb采用默认设置的独立配置。 ttlMonitorEnabled上的getParameter返回true,这样也很好。

这里有什么问题?

1 个答案:

答案 0 :(得分:1)

您的索引创建中存在拼写错误。

设置expiresAfterSeconds应为expireAfterSeconds