Capped collections are fixed-size collections that insert and retrieve documents based on insertion order. Capped collections work similarly to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.封顶集合是固定大小的集合,根据插入顺序插入和检索文档。封顶集合的工作方式类似于循环缓冲区:一旦集合填满了分配的空间,它就会通过覆盖集合中最旧的文档为新文档腾出空间。
Restrictions限制
Capped collections cannot be sharded.封顶集合不能分片。Capped collections are not supported in Stable API V1.在Stable API V1中不支持封顶集合。You cannot write to capped collections in transactions.您不能在事务中写入封顶集合。The$outaggregation pipeline stage cannot write results to a capped collection.$out聚合管道阶段无法将结果写入封顶集合。
Command Syntax命令语法
The following example creates a capped collection called 以下示例创建了一个名为log的封顶集合,最大大小为100000字节。log with a maximum size of 100,000 bytes.
db.createCollection( "log", { capped: true, size: 100000 } )For more information on creating capped collections, see 有关创建封顶集合的更多信息,请参阅createCollection() or create.createCollection()或create。
Use Cases用例
Generally, TTL (Time To Live) indexes offer better performance and more flexibility than capped collections. TTL indexes 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值从正常集合中删除数据。
Capped collections serialize write operations and therefore have worse concurrent insert, update, and delete performance than non-capped collections. Before you create a capped collection, consider if you can use a TTL index instead.封顶集合序列化写操作,因此与非封顶集合相比,其并发插入、更新和删除性能较差。在创建封顶集合之前,请考虑是否可以使用TTL索引。
The most common use case for a capped collection is to store log information. When the capped collection reaches its maximum size, old log entries are automatically overwritten with new entries.封顶集合最常见的用例是存储日志信息。当封顶集合达到其最大大小时,旧日志条目将自动被新条目覆盖。
Get Started开始使用
To create and query capped collections, see these pages:要创建和查询封顶集合,请参阅以下页面:
Behavior行为
Consider these behavioral details for capped collections.考虑封顶集合的这些行为细节。
Oplog Collection操作日志集合
The oplog.rs collection that stores a log of the operations in a replica set uses a capped collection.在副本集中存储操作日志的oplog.rs集合使用封顶集合。
Unlike other capped collections, the oplog can grow past its configured size limit to avoid deleting the 与其他封顶集合不同,oplog可以超过其配置的大小限制,以避免删除多数提交点。majority commit point.
Note
MongoDB rounds the capped size of the oplog up to the nearest integer multiple of 256, in bytes.MongoDB将oplog的封顶大小四舍五入到最接近的256的整数倍(以字节为单位)。
_id Index_id索引
Capped collections have an 默认情况下,封顶集合有一个_id field and an index on the _id field by default._id字段和一个关于_id字段的索引。
Updates更新
Avoid updating data in a capped collection. Because capped collections are fixed-size, updates can cause your data to expand beyond the collection's allocated space, which can cause unexpected behavior.避免更新封顶集合中的数据。因为封顶集合的大小是固定的,更新可能会导致您的数据扩展到集合的分配空间之外,这可能会导致意外行为。
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命令。
Tailable Cursor可尾随游标
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. As new documents are inserted into the capped collection, you can use the tailable cursor to continue retrieving documents.tail -f命令类似,可尾随游标“尾随”封顶集合的末尾。当新文档插入到封顶集合中时,您可以使用可尾随游标继续检索文档。
For information on creating a tailable cursor, see Tailable Cursors.有关创建可尾随游标的信息,请参阅可尾随游标。
Multiple Concurrent Writes多个并发写入
If there are concurrent writers to a capped collection, MongoDB does not guarantee that documents are returned in insertion order.如果一个封顶集合有并发写入程序,MongoDB不保证文档按插入顺序返回。
Read Concern Snapshot读取关注快照
Starting in MongoDB 8.0, you can use read concern 从MongoDB 8.0开始,您可以在封顶集合上使用读取关注"snapshot" on capped collections."snapshot"(快照)。