mongodb ttl索引

mongodb ttl索引

作用

所有可以通过指定data类型的数据值进行自动删除所谓的过期数据

创建索引

创建索引
collection 集合名
createTime 字段名,且类型为data
expireAfterSeconds 过期时间,单位是秒
db.collection.createIndex({"createTime":1},{expireAfterSeconds:6})

Demo

修改索引

collection 集合名
createTime ttl 索引字段 类型为data
expireAfterSeconds 过期时间,单位是秒
db.runCommand({ collMod: "collection", index: { keyPattern: { createTime: 1 },expireAfterSeconds: 7}})

Demo

查看索引

collection 集合名
db.collection.getIndexes()

Demo

删除索引

db.collection.dropIndex("索引名")

Demo

备注

插入数据命令Demo: db.test1.insert( {"_insert_time": new Date(),"logEvent": 2,"logMessage": "Success!"} )
mongdb 默认时区是UTC,晚北京时间8h

mongodb ttl索引

原文:https://www.cnblogs.com/MDD-Blog/p/15265726.html

以上是mongodb ttl索引的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>