- MongoDB官方文档中文版
 - MongoDB中文手册说明
 - MongoDB简介
 - 安装 MongoDB
 - The mongo Shell
 - MongoDB CRUD 操作
 - 聚合
 - 数据模型
 - 事务
 - 索引
 -  安全   
- 安全检查列表
 - 启用访问控制
 - 身份验证
 - 基于角色的访问控制
 - TLS / SSL(传输加密)
 - 静态加密
 - 客户端字段级加密
 - 审计
 - 网络和配置强化
 - 实现字段级别修订
 - 安全参考
 - 附录
 
 - 变更流
 - 复制
 - 分片   
- 分片键
 - 哈希分片
 - 范围分片
 - 区
 - 管理分片区
 - 按位置细分数据
 - 用于更改SLA或SLO的分层硬件
 - 按应用或客户细分数据
 - 仅插入工作负载的分布式本地写入
 - 管理分片区
 - 使用块进行数据分区
 - 在分片集群中拆分数据块
 
 - 管理
 -  存储   
-  存储引擎   
- 日志记录
 - 管理日志记录   
- GridFS
 
 
 - FAQ:MongoDB 存储
 
 -  存储引擎   
 - 参考   
- 运算符
 - 查询与映射运算符
 - 更新运算符
 - 聚合管道阶段
 -  聚合管道操作符   
- $abs (aggregation)
 - $acos (aggregation)
 - $acosh (aggregation)
 - $add (aggregation)
 - $addToSet (aggregation)
 - $allElementsTrue (aggregation)
 - $and (aggregation)
 - $anyElementTrue (aggregation)
 - $arrayElemAt (aggregation)
 - $arrayToObject (aggregation)
 - $asin (aggregation)
 - $asinh (aggregation)
 - $atan (aggregation)
 - $atan2 (aggregation)
 - $atanh (aggregation)
 - $avg (aggregation)
 - $ceil (aggregation)
 - $cmp (aggregation)
 - $concat (aggregation)
 - $concatArrays (aggregation)
 - $cond (aggregation)
 - $convert (aggregation)
 - $cos (aggregation)
 - $dateFromParts (aggregation)
 - $dateToParts (aggregation)
 - $dateFromString (aggregation)
 - $literal (aggregation)
 
 - 查询修饰符
 - 数据库命令
 - 聚合命令
 - 地理空间命令
 - 查询和写操作命令
 - 查询计划缓存命令
 - 认证命令
 - 用户管理命令
 - 角色管理命令
 - 复制命令
 - 分片命令
 - 会话命令
 - 管理命令
 - 诊断命令
 - 免费监控命令
 - 系统事件审计命令
 - mongo Shell 方法
 -  集合方法   
- db.collection.aggregate()
 - db.collection.bulkWrite()
 - db.collection.copyTo()
 - db.collection.count()
 - db.collection.countDocuments()
 - db.collection.estimatedDocumentCount()
 - db.collection.createIndex()
 - db.collection.createIndexes()
 - db.collection.dataSize()
 - db.collection.deleteOne()
 - db.collection.deleteMany()
 - db.collection.distinct()
 - db.collection.drop()
 - db.collection.dropIndex()
 - db.collection.dropIndexes()
 - db.collection.ensureIndex()
 - db.collection.explain()
 - db.collection.find()
 - db.collection.findAndModify()
 - db.collection.findOne()
 - db.collection.findOneAndDelete()
 - db.collection.findOneAndReplace()
 - db.collection.findOneAndUpdate()
 - db.collection.getIndexes()
 - db.collection.getShardDistribution()
 - db.collection.getShardVersion()
 - db.collection.insert()
 - db.collection.insertOne()
 - db.collection.insertMany()
 - db.collection.isCapped()
 - db.collection.latencyStats()
 - db.collection.mapReduce()
 - db.collection.reIndex()
 - db.collection.remove()
 - db.collection.renameCollection()
 - db.collection.replaceOne()
 - db.collection.save()
 - db.collection.stats()
 - db.collection.storageSize()
 - db.collection.totalIndexSize()
 - db.collection.totalSize()
 - db.collection.update()
 - db.collection.updateOne()
 - db.collection.updateMany()
 - db.collection.watch()
 - db.collection.validate()
 
 - 词汇表
 - 默认的MongoDB端口
 - 默认的MongoDB读/写关注
 - 服务器会话
 
 - MongoDB驱动
 - FAQ
 - 联系我们
 - 更多资料   
- [快学Mongo]
 - [Mongo问题讨论区]
 - [Mongo 驱动使用手册]
 
 - 本书使用 GitBook 发布
 
通过设置TTL使集合中的数据过期
通过设置TTL使集合中的数据过期
在本页面
本文介绍了MongoDB的“生存时间”或TTL集合特性。TTL集合使它可能存储数据在MongoDB和有' mongod '自动删除数据后指定的秒数或在一个特定的时钟时间。
数据过期对于某些类别的信息很有用,包括机器生成的事件数据、日志和会话信息,这些信息只需要持续有限的一段时间。
一个特殊的TTL索引属性支持TTL集合的实现。TTL特性依赖于' mongod '中的一个后台线程,该线程读取索引中的日期类型值并从集合中删除过期的document。
程序
若要创建TTL索引,请在其值为日期或包含日期值的数组 的字段上使用db.collection.createIndex()带有expireAfterSeconds选项 的 方法 。
请注意
TTL索引是单个字段索引。复合索引不支持TTL属性。有关TTL索引的更多信息,请参见TTL索引。
You can modify the expireAfterSeconds of an existing TTL index using the collMod command.
在指定的秒数之后使文档过期
要在索引字段之后的指定秒数过期数据,在保存BSON日期类型值或BSON日期类型对象数组的字段上创建一个TTL索引,并在expireAfterSeconds字段中指定一个正的非零值。当' expireAfterSeconds '字段中的秒数超过索引字段中指定的时间时,文档将过期。
例如,下面的操作在“log_events”集合的“createdAt”字段上创建一个索引,并指定“expireAfterSeconds”的值“3600”,将过期时间设置为“createdAt”指定的时间之后一小时。
db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
当向“log_events”集合添加文档时,将“createdAt”字段设置为当前时间:
db.log_events.insert( {
   "createdAt": new Date(),
   "logEvent": 2,
   "logMessage": "Success!"
} )
当文档的createdAt值大于expireAfterSeconds中指定的秒数时,MongoDB将自动从log_events集合中删除文档。
在特定的时钟时间时过期文档
要使文档在特定的时钟时间过期,首先在一个字段上创建一个TTL索引,该字段包含BSON日期类型的值或BSON日期类型对象的数组和指定expireAfterSeconds值为0。对于集合中的每个文档,将索引日期字段设置为与文档应该过期的时间对应的值。如果索引日期字段包含过去的日期,MongoDB认为文档过期。
例如,以下操作在“log_events”集合的“expireAt”字段上创建一个索引,并指定“expireAfterSeconds”的值为“0”:
db.log_events.createIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } )
对于每个文档,将expireAt的值设置为与文档应该过期的时间对应。例如,下面的 insert() 操作添加了一个应该在July 22, 2013 14:00:00到期的文档。
db.log_events.insert( {
   "expireAt": new Date('July 22, 2013 14:00:00'),
   "logEvent": 2,
   "logMessage": "Success!"
} )
当文档' expireAt的值大于expireAfterSeconds中指定的秒数时,MongoDB将自动从log_events集合中删除文档。在本例中是“0”秒。因此,数据在指定的expireAt值过期。
Copyright © 上海锦木信息技术有限公司 all right reserved,由 MongoDB汉化小组 提供技术支持文件修订时间: 2020-10-11 20:53:05
