在 mongodb 2.6.7 中使用 TTL 的文档不会过期
Documents not expiring using TTL in mongodb 2.6.7
我正在尝试 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() returns 也是一个空数组。
这是一个开发环境,因此 mongodb 处于具有默认设置的独立配置中。 ttlMonitorEnabled 上的 getParameter returns true 这样也很好。
这里有什么问题?
您创建索引时出现错字。
设置 expiresAfterSeconds
应该是 expireAfterSeconds
。
我正在尝试 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() returns 也是一个空数组。
这是一个开发环境,因此 mongodb 处于具有默认设置的独立配置中。 ttlMonitorEnabled 上的 getParameter returns true 这样也很好。
这里有什么问题?
您创建索引时出现错字。
设置 expiresAfterSeconds
应该是 expireAfterSeconds
。