New in version 6.0.版本6.0中的新功能。
To change the maximum number of documents in a capped collection, use the 要更改封顶集合中的最大文档数,请使用collMod command's cappedMax option.collMod命令的cappedMax选项。
If如果cappedMaxis less than or equal to0, there is no maximum document limit.cappedMax小于或等于0,则没有最大文档限制。If如果cappedMaxis less than the current number of documents in the collection, MongoDB removes the excess documents on the next insert operation.cappedMax小于集合中的当前文档数,MongoDB将在下一次插入操作中删除多余的文档。
About this Task关于此任务
Generally, TTL (Time To Live) indexes offer better performance and more flexibility than capped collections. TTL indexes expire and remove data from normal collections based on the value of a date-typed field and a TTL value for the index.通常,TTL(生存时间)索引比上限集合提供更好的性能和更大的灵活性。TTL索引过期,并根据日期类型字段的值和索引的TTL值从正常集合中删除数据。
Capped collections serialize write operations and therefore have worse concurrent insert, update, and delete performance than non-capped collections. Before you create a capped collection, consider if you can use a TTL index instead.有上限的集合序列化写操作,因此与无上限的集合相比,其并发插入、更新和删除性能较差。在创建带上限的集合之前,请考虑是否可以使用TTL索引。
Before you Begin开始之前
Create a capped collection called 创建一个名为log的上限集合,最多可以存储20000个文档:log that can store a maximum of 20,000 documents:
db.createCollection( "log", { capped: true, size: 5242880, max: 20000 } )Steps步骤
Run the following command to set the maximum number of documents in the 运行以下命令,将log collection to 5,000:log集合中的最大文档数设置为5000:
db.runCommand( { collMod: "log", cappedMax: 5000 } )