Database Manual / Introduction / Databases & Collections / Capped Collections

Change Maximum Documents in a Capped Collection更改上限集合中的最大文档数

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选项。

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 can store a maximum of 20,000 documents:创建一个名为log的上限集合,最多可以存储20000个文档:

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

Learn More了解更多