我已经执行了这个命令来在 mongodb 上设置一个 TTL 索引,
db.sessions.ensureIndex({'expiration':1},{"expireAfterSeconds": 30})
但 4 天后,我发现这些文件没有被删除。
我已经确认命令和文档的字段是正确的。
我不知道如何修复它。
执行 db.serverStatus() 后,我得到
本地时间是 2015-01-16 11:03:05.554+08:00
以下是我收藏的一些信息
db.sessions.getIndexes()
{
"0" : {
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "meta.sessions"
},
"1" : {
"v" : 1,
"key" : {
"expiration" : 1
},
"name" : "expiration_1",
"ns" : "meta.sessions",
**"expireAfterSeconds" : 30**
}
}
db.sessions.find()
/* 0 */
{
"_id" : ObjectId("54b4c2e0f840238ca1436788"),
"data" : ...,
"expiration" : **ISODate("2015-01-13T16:02:33.947+08:00"),**
"sid" : "..."
}
/* 1 */
{
"_id" : ObjectId("54b4c333f840238ca1436789"),
"data" : ...,
"expiration" : ISODate("2015-01-13T16:06:56.942+08:00"),
"sid" : ".."
}
/* ... */
最佳答案
当您在前台创建 TTL 索引时(就像您所做的那样),一旦索引完成构建,MongoDB 就会开始删除过期的文档。最好到tail -f mongod.log
在索引创建期间跟踪进度。如果出现问题,您可能希望删除并重新创建索引。
如果索引是在后台创建的,那么 TTL 线程可以在索引构建时开始删除文档。
删除过期文档的 TTL 线程每 60 秒运行一次。
如果您在从副本集中取出并以独立模式运行的副本上创建了索引,则将创建索引,但在您重新加入(或删除副本集)配置之前,不会删除文档。如果是这种情况,您可能会在 mongod.log
中得到类似的信息。** WARNING: mongod started without --replSet yet 1 documents are
** present in local.system.replset
** Restart with --replSet unless you are doing maintenance and no other
** clients are connected.
** The TTL collection monitor will not start because of this.
** For more info see http://dochub.mongodb.org/core/ttlcollections
关于mongodb TTL 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27976726/