New in version 6.0.版本6.0中的新功能。
To change the size of a capped collection, use the 要更改封顶集合的大小,请使用collMod command's cappedSize option. collMod命令的cappedSize选项。cappedSize is specified in bytes, and must be greater than 0 and less than or equal to 1024^5 (1 PB).cappedSize以字节为单位指定,必须大于0且小于等于1024^5(1 PB)。
If 如果cappedSize is less than the current size of the collection, MongoDB removes the excess documents on the next insert operation.cappedSize小于集合的当前大小,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 that has a maximum size of 2,621,440 bytes:log的上限集合,其最大大小为2621440字节:
db.createCollection( "log", { capped: true, size: 2621440 } )Steps步骤
Run the following command to set the maximum size of the 运行以下命令,将log collection to 5,242,880 bytes:log集合的最大大小设置为5242880字节:
db.runCommand( { collMod: "log", cappedSize: 5242880 } )