Database Manual / Introduction / Databases & Collections / Capped Collections

Change the Size of a Capped Collection更改封顶集合的大小

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 } )

Learn More了解更多