mongodb 设置自动删除过期数据

来源:互联网 发布:vb数据库源代码 编辑:程序博客网 时间:2024/05/21 06:41

Time To Live(TTL) 集合

MongoDB 2.2 引入一个新特性–TTL 集合,TTL集合支持失效时间设置,或者在某个特定时间, 
集合自动清除超时文档,者用来保存一个诸如session会话信息的时候非常有用。

如果想使用TTL集合,用用到 expireAfterSeconds 选项

mongo

官网使用文档设置方法:

Expire Documents after a Specified Number of Seconds

首先创建索引,设置过期时间

db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
  • 1
  • 2
  • 1
  • 2

然后存储数据入库

db.log_events.insert( {   "createdAt": new Date(),   "logEvent": 2,   "logMessage": "Success!"} )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

mongodb 会在 createdAt 数值大于 expireAfterSeconds 指定的值。

Expire Documents at a Specific Clock Time

与上面的设置类似 
首先建立索引,设置 expireAfterSeconds 为 0

db.log_events.createIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } )
  • 1
  • 2
  • 1
  • 2

然后存储数据

db.log_events.insert( {   "expireAt": new Date('July 22, 2013 14:00:00'),   "logEvent": 2,   "logMessage": "Success!"} )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

expireAt 的值为特定的时间值,等时间到达expireAt的值时,这个文档就 失效了。

pymongo

由于自己使用Python进行mongodb的使用,程序如下 
1 设置文档在66秒后过期。

from pymongo import MongoClientimport pymongoimport datetimeclient = MongoClient("10.168.99.118", 27017)collection = client.test.expirecollection.create_index([("time", pymongo.ASCENDING)], expireAfterSeconds=66)data = {   "one": 1,   "two": 235,   "time": datetime.datetime.utcnow(),}collection.insert(data)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2 设置特定时间

from pymongo import MongoClientimport pymongoimport datetimeclient = MongoClient("10.168.99.118", 27017)collection = client.test.expirecollection.create_index([("time", pymongo.ASCENDING)], expireAfterSeconds=0)data = {   "one": 1,   "two": 235,   "time": datetime.datetime.utcnow()+ datetime.timedelta(seconds=66),}collection.insert(data)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

注意,上面的时间使用的是datetime.datetime.utcnow() 而不是 datetime.datetime.now(), 这两者的时间是有差别的,数据存入mongodb的客户端中可以看得到。 
现在时间是 2017-02-09 14:36 
第一条数据使用 datetime.datetime.utcnow() 的时间 
第二条数据使用 datetime.datetime.now() 的时间

这里写图片描述

所以,第二条数据会在晚上 10点多才会过期。

mongoengine

自己使用的Django中的数据库也是mongodb。

from mongoengine import *import datetimeimport timeconnect('test', host='10.168.99.118', port=27017)class Session(Document):   created = DateTimeField(default=datetime.datetime.utcnow()-datetime.timedelta(seconds=60))   last = DateTimeField(default=datetime.datetime.utcnow())   num = IntField(unique=True)   count = IntField(default=0)   meta = {       'indexes': [           {'fields': ['created'], 'expireAfterSeconds': 66}       ]   }s = Session.objects(num=51)if len(s)>0:   print(s[0].count)else:   b = s[0]   b.num = 51   b.count = 2   b.save()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

修改成特定时间过期的方式与pymongo中的类似。

参考文档: 
python - Mongodb TTL expires documents early 
http://www.itgo.me/a/8424428520865988282/mongodb-ttl-expires-documents-early

MongoDB自动删除过期数据–TTL索引 
http://blog.csdn.net/jianlong727/article/details/54631124

0 0
原创粉丝点击