Docs HomeMongoDB Manual

Capped Collections封顶集合

Overview概述

Capped collections封顶集合 are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. 是固定大小的集合,支持基于插入顺序插入和检索文档的高吞吐量操作。Capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.封顶集合的工作方式类似于循环缓冲区:一旦集合填满了分配的空间,它就会覆盖集合中最旧的文档,为新文档腾出空间。

See createCollection() or create for more information on creating capped collections.请参阅createCollection()create以获取有关创建封顶集合的更多信息。

Tip

As an alternative to capped collections, consider MongoDB's TTL (Time To Live) indexes. 作为封顶集合的替代方案,可以考虑MongoDB的TTL(生存时间)索引As described in Expire Data from Collections by Setting TTL, these indexes allow you to 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 indexes are not compatible with capped collections.TTL索引与封顶集合不兼容。

Behavior行为

Insertion Order插入顺序

Capped collections guarantee preservation of the insertion order. 封顶集合保证了插入顺序的保存。As a result, queries do not need an index to return documents in insertion order. 因此,查询不需要索引就可以按插入顺序返回文档。Without this indexing overhead, capped collections can support higher insertion throughput.如果没有这种索引开销,封顶集合可以支持更高的插入吞吐量。

Automatic Removal of Oldest Documents自动删除最旧的文档

To make room for new documents, capped collections automatically remove the oldest documents in the collection without requiring scripts or explicit remove operations.为了为新文档腾出空间,封顶集合会自动删除集合中最旧的文档,而不需要脚本或显式删除操作。

Consider the following potential use cases for capped collections:考虑以下封顶集合的潜在用例:

  • Store log information generated by high-volume systems. 存储大容量系统生成的日志信息。Inserting documents in a capped collection without an index is close to the speed of writing log information directly to a file system. Furthermore, the built-in first-in-first-out property maintains the order of events, while managing storage use. 在没有索引的封顶集合中插入文档的速度接近于将日志信息直接写入文件系统的速度。此外,内置的先进先出属性在管理存储使用的同时维护事件的顺序。For example, the oplog uses a capped collection.例如,oplog使用了一个封顶集合。
  • Cache small amounts of data in a capped collections. 在封顶集合中缓存少量数据。Since caches are read rather than write heavy, you would either need to ensure that this collection always remains in the working set (i.e. in RAM) or accept some write penalty for the required index or indexes.由于缓存是读而不是写重的,因此您需要确保此集合始终保留在工作集中(即RAM中),或者为所需的一个或多个索引接受一些写惩罚。

Oplog CollectionOplog集合

The oplog.rs collection that stores a log of the operations in a replica set uses a capped collection.副本集中存储操作日志的oplog.rs集合使用封顶集合。

Starting in MongoDB 4.0, unlike other capped collections, the oplog can grow past its configured size limit to avoid deleting the majority commit point.从MongoDB 4.0开始,与其他封顶集合不同,oplog可以超过其配置的大小限制,以避免删除大多数提交点

Note

MongoDB rounds the capped size of the oplog up to the nearest integer multiple of 256, in bytes.MongoDB将oplog的上限大小四舍五入到256的整数倍,单位为字节。

_id Index指数

Capped collections have an _id field and an index on the _id field by default.默认情况下,封顶集合有一个_id字段和一个_id字段上的索引。

Restrictions and Recommendations限制和建议

Reads读取

Starting in MongoDB 5.0, you cannot use read concern "snapshot" when reading from a capped collection.从MongoDB 5.0开始,在从封顶集合中读取时,不能使用读取关注点"snapshot"

Updates更新

If you plan to update documents in a capped collection, create an index so that these update operations do not require a collection scan.如果计划更新封顶集合中的文档,请创建索引,以便这些更新操作不需要扫描集合。

Sharding分片

You cannot shard a capped collection.你不能分片一个封顶集合。

Query Efficiency查询效率

Use natural ordering to retrieve the most recently inserted elements from the collection efficiently. 使用自然排序可以有效地从集合中检索最近插入的元素。This is similar to using the tail command on a log file.这类似于在日志文件上使用tail命令。

Aggregation 聚合$out

The aggregation pipeline stage $out cannot write results to a capped collection.聚合管道阶段$out无法将结果写入封顶集合。

Transactions事务

Starting in MongoDB 4.2, you cannot write to capped collections in transactions.从MongoDB 4.2开始,您不能在事务中写入封顶集合。

Stable API

Capped collections are not supported in Stable API V1.Stable API V1中不支持封顶集合。

Procedures过程

Create a Capped Collection创建封顶集合

You must create capped collections explicitly using the db.createCollection() method, which is a mongosh helper for the create command. 必须使用db.createCollection()方法显式创建封顶集合,该方法是create命令的mongosh助手。When creating a capped collection you must specify the maximum size of the collection in bytes, which MongoDB will pre-allocate for the collection. 创建封顶集合时,必须指定集合的最大大小(以字节为单位),MongoDB将为该集合预先分配该大小。The size of the capped collection includes a small amount of space for internal overhead.封顶集合的大小包括用于内部开销的少量空间。

db.createCollection( "log", { capped: true, size: 100000 } )
Note

The value that you provide for the size field must be greater than 0 and less than or equal to 1024^5 (1 PB). size字段提供的值必须大于0且小于或等于1024^5(1 PB)。MongoDB rounds the size of all capped collections up to the nearest integer multiple of 256, in bytes.MongoDB将所有封顶集合的size四舍五入到最近的256的整数倍(以字节为单位)。

Additionally, you may also specify a maximum number of documents for the collection using the max field as in the following document:此外,您还可以使用以下文档中的max字段为集合指定文档数上限:

db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )
Important

The size field is always required, even when you specify the max number of documents. size字段始终是必需的,即使指定了max文档数也是如此。MongoDB removes older documents if a collection reaches the maximum size limit before it reaches the maximum document count.如果集合在达到最大文档数之前达到最大大小限制,MongoDB将删除旧文档。

Query a Capped Collection查询封顶集合

If you perform a find() on a capped collection with no ordering specified, MongoDB guarantees that the ordering of results is the same as the insertion order.如果在没有指定排序的情况下对封顶集合执行find(),MongoDB会保证结果的排序与插入顺序相同。

To retrieve documents in reverse insertion order, issue find() along with the sort() method with the $natural parameter set to -1, as shown in the following example:要按反向插入顺序检索文档,请发出find()sort()方法,$natural参数设置为-1,如以下示例所示:

db.cappedCollection.find().sort( { $natural: -1 } )

Check if a Collection is Capped检查集合是否封顶

Use the isCapped() method to determine if a collection is capped, as follows:使用isCapped()方法确定集合是否已封顶,如下所示:

db.collection.isCapped()

Convert a Collection to Capped将集合转换为封顶集合

You can convert a non-capped collection to a capped collection with the convertToCapped command:可以使用convertToCapped命令将未封顶集合转换为封顶集合:

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

The size parameter specifies the size of the capped collection in bytes.size参数指定封顶集合的大小(以字节为单位)。

This holds a database exclusive lock for the duration of the operation. 这将在操作期间保留数据库独占锁。Other operations which lock the same database will be blocked until the operation completes. 锁定同一数据库的其他操作将被阻止,直到操作完成。See What locks are taken by some common client operations? for operations that lock the database.请参阅一些常见的客户端操作使用哪些锁?以了解锁定数据库的操作。

Change a Capped Collection's Size更改封顶集合的大小

New in version 6.0.

You can resize a capped collection using the collMod command's cappedSize option to set the cappedSize in bytes. 您可以使用collMod命令的cappedSize选项来设置cappedSize(以字节为单位),从而调整封顶集合的大小。cappedSize must be greater than 0 and less than or equal to 1024^5 (1 PB).必须大于0且小于或等于1024^5(1 PB)。

Note

Before you can resize a capped collection, you must have already set the featureCompatibilityVersion to at least version "6.0".在调整封顶集合的大小之前,必须已将featureCompatibilityVersion设置为至少"6.0"版本。

For example, the following command sets the maximum size of the "log" capped collection to 100000 bytes:例如,以下命令将"log"封顶集合的最大大小设置为100000字节:

db.runCommand( { collMod: "log", cappedSize: 100000 } )

Change the Maximum Number of Documents in a Capped Collection更改封顶集合中的最大文档数

New in version 6.0.

To change the maximum number of documents in a capped collection, use the collMod command's cappedMax option. 要更改封顶集合中的最大文档数,请使用collMod命令的cappedMax选项。If cappedMax is less than or equal to 0, there is no maximum document limit. 如果cappedMax小于或等于0,则没有最大文档限制。If cappedMax is less than the current number of documents in the collection, MongoDB removes the excess documents on the next insert operation.如果cappedMax小于集合中当前的文档数,MongoDB将在下一次插入操作中删除多余的文档。

For example, the following command sets the maximum number of documents in the "log" capped collection to 500:例如,以下命令将"log"封顶集合中的最大文档数设置为500:

db.runCommand( { collMod: "log", cappedMax: 500 } )

Tailable Cursor可尾随游标

You can use a tailable cursor with capped collections. 您可以使用封顶集合的可裁剪游标Similar to the Unix tail -f command, the tailable cursor "tails" the end of a capped collection. 类似于Unix的tail -f命令,可尾随游标“尾随”一个带帽集合的末尾。As new documents are inserted into the capped collection, you can use the tailable cursor to continue retrieving documents.当新文档插入到带帽集合中时,可以使用可裁剪游标继续检索文档。

See Tailable Cursors for information on creating a tailable cursor.有关创建可尾随游标的信息,请参阅可尾随游标