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
以了解有关创建封顶集合的更多信息。
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.与封顶集合不兼容。
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集合使用capped集合。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可以增长到超过其配置的大小限制,以避免删除多数提交点。majority commit point
.
_id
IndexCapped collections have an 默认情况下,封顶集合有一个_id
field and an index on the _id
field by default._id
字段和一个_id
字段上的索引。
Starting in MongoDB 5.0, you cannot use read concern 从MongoDB 5.0开始,在读取封顶集合时,不能使用读取关注点"snapshot"
when reading from a capped collection."snapshot"
。
If you plan to update documents in a capped collection, create an index so that these update operations do not require a collection scan.如果计划更新封顶集合中的文档,请创建索引,以便这些更新操作不需要集合扫描。
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 similar to using the 这类似于在日志文件上使用tail
command on a log file.tail
命令。
$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开始,您不能在事务中写入封顶集合。
Capped collections are not supported in Stable API V1.稳定API V1不支持封顶集合。
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 } )
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将删除旧文档。
db.createCollection()
and 和create
.。
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 } )
Use the 使用isCapped()
method to determine if a collection is capped, as follows:isCapped()
方法确定集合是否被封顶,如下所示:
db.collection.isCapped()
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.参阅一些常见的客户端操作使用了哪些锁?用于锁定数据库的操作。
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.有关创建可裁剪游标的信息,请参阅可裁剪游标。