On this page本页内容
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.与封顶集合不兼容。
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.如果没有这种索引开销,封顶集合可以支持更高的插入吞吐量。
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:考虑以下封顶集合的潜在用例:
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 从MongoDB 4.0开始,oplog与其他capped集合不同,oplog可以增长到超过其配置的大小限制,以避免删除majority commit point
.majority commit point
。
_id
Capped collections have an 默认情况下,封顶集合具有_id
field and an index on the _id
field by default._id
字段和_id
字段上的索引。
If you plan to update documents in a capped collection, create an index so that these update operations do not require a collection scan.如果计划更新封顶集合中的文档,请创建索引,以便这些更新操作不需要集合扫描。
Changed in version 3.2.在版本3.2中更改。
If an update or a replacement operation changes the document size, the operation will fail.如果更新或替换操作更改了文档大小,则操作将失败。
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()
方法删除集合,然后重新创建封顶集合。
You cannot shard a capped collection.你不能分割一个封顶集合。
Use natural ordering to retrieve the most recently inserted elements from the collection efficiently.使用自然排序可以有效地从集合中检索最近插入的元素。This is (somewhat) analogous to tail on a log file.这(有点)类似于日志文件的尾部。
$out
¶The aggregation pipeline stage 聚合管道阶段$out
cannot write results to a capped collection.$out
无法将结果写入到已封顶的集合。
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.事务中仍支持从封顶集合读取。
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.封顶集合的大小为内部开销提供了少量空间。
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
字段指定集合的最大文档数,如以下文档中所示:
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将删除旧文档。
See
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
,如下例所示:
Use the 使用isCapped()
method to determine if a collection is capped, as follows:isCapped()
方法确定集合是否有上限,如下所示:
You can convert a non-capped collection to a capped collection with the 可以使用convertToCapped
command:convertToCapped
命令将非封顶集合转换为封顶集合:
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.查看一些常见的客户端操作使用了哪些锁用于锁定数据库的操作?。
You can use a tailable cursor with capped collections. 您可以将可裁剪游标与封顶集合一起使用。Similar to the Unix 与Unix tail -f
command, the tailable cursor “tails” the end of a capped collection. 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.有关创建可剪裁游标的信息,请参阅可剪裁游标。