Database Manual / Introduction / Databases & Collections / Capped Collections

Convert a Collection to Capped将集合转换为大写

To convert a non-capped collection to a capped collection, use the convertToCapped database command.要将非封顶集合转换为封顶集合,请使用convertToCapped数据库命令。

The convertToCapped command holds a database-exclusive lock for the duration of the operation. Other operations that lock the same database are blocked until the convertToCapped operation completes.convertToApped命令在操作期间持有数据库独占锁。锁定同一数据库的其他操作将被阻止,直到convertToApped操作完成。

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 non-capped collection called log2:创建一个名为log2的封顶集合:

db.createCollection("log2")

Steps步骤

1

Convert the collection to a capped collection将集合转换为封顶集合

To convert the log2 collection to a capped collection, run the convertToCapped command:要将log2集合转换为封顶集合,请运行convertToCapped命令:

db.runCommand( {
convertToCapped: "log2",
size: 100000
} )

The log2 collection has a maximum size of 100,000 bytes.log2集合的最大大小为100000字节。

2

Confirm that the collection is capped确认收款已封顶

To confirm that the log2 collection is now capped, use the isCapped() method:要确认log2集合现在已封顶,请使用isCapped()方法:

db.log2.isCapped()
   true

Learn More了解更多