我执行了此命令,以在mongodb上设置TTL索引 db.sessions.ensureIndex({'expiration':1},{“ expireAfterSeconds”:30})
但是四天后,我发现这些文件没有被删除。我已经确认命令和文档的字段是正确的。
我不知道该如何解决。
执行db.serverStatus()之后,我得到的 localTime是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**
}
}
Run Code Online (Sandbox Code Playgroud)
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" : ".."
}
/* ... */
Run Code Online (Sandbox Code Playgroud)
小智 5
要使集合中的数据过期(在版本3.2中进行测试),必须创建索引:
db.my_collection.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
Run Code Online (Sandbox Code Playgroud)
此后,您插入此集合中的每个文档都必须具有“ createdAt”和当前日期:
db.my_collection.insert( {
"createdAt": new Date(),
"dataExample": 2,
"Message": "Success!"
} )
Run Code Online (Sandbox Code Playgroud)
当createdAt值+ expireAfterSeconds值到达时,文档将被删除。注意:默认情况下,MongoDB中的此后台任务每60秒执行一次。
当您在前台创建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
| 归档时间: |
|
| 查看次数: |
6544 次 |
| 最近记录: |