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(Time to Live)索引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集合在副本集中存储操作日志的集合使用封顶集合。 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与其他capped集合不同,oplog可以增长到超过其配置的大小限制,以避免删除majority commit point

_id Index索引

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

Restrictions and Recommendations限制和建议

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 Size文档大小

Changed in version 3.2.在版本3.2中更改。

If an update or a replacement operation changes the document size, the operation will fail.如果更新或替换操作更改了文档大小,则操作将失败。

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 (somewhat) analogous to tail on a log file.这(有点)类似于日志文件的尾部。

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开始,您不能在事务中写入封顶集合。Reads from capped collections are still supported in transactions.事务中仍支持从封顶集合读取。

Procedures过程

Create a Capped Collection创建封顶集合

You must create capped collections explicitly using the db.createCollection() method, which is a helper in the mongo shell for the create command. 必须使用db.createCollection()方法显式创建封顶集合,该方法是用于create命令的mongo shell中的帮助程序。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将删除旧文档。

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.有关创建可剪裁游标的信息,请参阅可剪裁游标