Capped Collections封顶集合

On this page本页内容

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.与封顶集合不兼容。

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.此外,内置的先进先出属性在管理存储使用的同时维护事件的顺序。
  • 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中),或者对所需的一个或多个索引接受一些写惩罚。

For example, the oplog.rs collection that stores a log of the operations in a replica set uses a capped collection. 例如,在副本集中存储操作日志的oplog.rs集合使用capped集合。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可以增长到超过其配置的大小限制,以避免删除多数提交点

_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.如果计划更新封顶集合中的文档,请创建索引,以便这些更新操作不需要集合扫描。

Document Deletion文档删除

You cannot delete documents from a capped collection. 您不能从封顶集合中删除文档。To remove all documents from a collection, use the drop() method to drop the collection and recreate the capped collection.要从集合中删除所有文档,请使用drop()方法删除集合并重新创建封顶集合。

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稳定的API

Capped collections are not supported in Stable API V1.稳定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 } )

If the size field is less than or equal to 4096, then the collection will have a cap of 4096 bytes. 如果size字段小于或等于4096,则集合的上限为4096字节。Otherwise, MongoDB will raise the provided size to make it an integer multiple of 256.否则,MongoDB将提高提供的大小,使其成为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 argument is always required, even when you specify max number of documents. size参数始终是必需的,即使指定了max文档数。MongoDB will remove older documents if a collection reaches the maximum size limit before it reaches the maximum document count.如果集合在达到最大文档数之前达到最大大小限制,MongoDB将删除旧文档。

Tip提示
See: 参阅

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.参阅一些常见的客户端操作使用了哪些锁?用于锁定数据库的操作。

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.在将新文档插入capped集合时,可以使用可裁剪游标继续检索文档。

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

←  On-Demand Materialized ViewsTime Series Collections →