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.
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:
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.
For example, the oplog.rs collection that stores a log of the operations in a replica set uses a capped collection. 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.
_id
IndexCapped collections have an _id
field and an index on the _id
field by default.
Starting in MongoDB 5.0, you cannot use read concern "snapshot" when reading from a capped collection.
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 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.
$out
The aggregation pipeline stage $out cannot write results to a capped collection.
Starting in MongoDB 4.2, you cannot write to capped collections in transactions.
Capped collections are not supported in Stable API V1.
You must create capped collections explicitly using the db.createCollection() method, which is a mongosh helper for the create command. When creating a capped collection you must specify the maximum size of the collection in bytes, which MongoDB will pre-allocate for the collection. 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. Otherwise, MongoDB will raise the provided size to make it an integer multiple of 256.
Additionally, you may also specify a maximum number of documents for the collection using the max
field as in the following document:
db.createCollection("log", { capped : true, size : 5242880, max : 5000 } ) |
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.
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:
db.cappedCollection.find().sort( { $natural: -1 } ) |
Use the isCapped() method to determine if a collection is capped, as follows:
db.collection.isCapped() |
You can convert a non-capped collection to a capped collection with the convertToCapped command:
db.runCommand({"convertToCapped": "mycoll", size: 100000}); |
The size
parameter specifies the size of the capped collection in bytes.
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.
New in version 6.0.
You can resize a capped collection using the collMod command's cappedSize
option to set the cappedSize
in bytes. cappedSize
must be greater than 0
and less than 1e+15
(1 PB).
For example, the following command sets the maximum size of the "log"
capped collection to 100000 bytes:
db.runCommand( { collMod: "log", cappedSize: 100000 } ) |
New in version 6.0.
To change the maximum number of documents in a capped collection, use the collMod command's cappedMax
option. If cappedMax
is less than or equal to 0
, there is no maximum document limit.
For example, the following command sets the maximum number of documents in the "log"
capped collection to 500:
db.runCommand( { collMod: "log", cappedMax: 500 } ) |
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. As new documents are inserted into the capped collection, you can use the tailable cursor to continue retrieving documents.
See Tailable Cursors for information on creating a tailable cursor.
****************** we had a big issue in the project in the past , and it caused the tailable cursor cannot be used for streaming data . so if you are using node.js streaming data , please enable the capped connection feature .
sort by Moshow郑锴的CSDNhttps://zhengkai.blog.csdn.net/
MongoDB 固定集合(Capped Collections)是性能出色且有着固定大小的集合,对于大小固定,我们可以想象其就像一个环形队列,当集合空间用完后,再插入的元素就会覆盖最初始的头部的元素!
我们通过createCollection来创建一个固定集合,且capped选项设置为true:
>db.createCollection("cappedLogCollection",{capped:true,size:10000})
还可以指定文档个数,加上max:1000属性:
>db.createCollection("cappedLogCollection",{capped:true,size:10000,max:1000})
判断集合是否为固定集合:
>db.cappedLogCollection.isCapped()
如果需要将已存在的集合转换为固定集合可以使用以下命令:
>db.runCommand({"convertToCapped":"posts",size:10000})
以上代码将我们已存在的 posts 集合转换为固定集合。
***补充
db.createCollection("cappedLogCollection",{capped:true,size:10000,max:1000})
size 是整个集合空间大小,单位为【字节】
max 是集合文档个数上线,单位是【个】
如果空间大小到达上限,则插入下一个文档时,会覆盖第一个文档;如果文档个数到达上限,同样插入下一个文档时,会覆盖第一个文档。两个参数上限判断取的是【与】的逻辑。
固定集合文档按照插入顺序储存的,默认情况下查询就是按照插入顺序返回的,也可以使用$natural调整返回顺序。
>db.cappedLogCollection.find().sort({$natural:-1})
可以插入及更新,但更新不能超出collection的大小,否则更新失败,不允许删除,但是可以调用drop()删除集合中的所有行,但是drop后需要显式地重建集合。
在32位机子上一个cappped collection的最大值约为482.5M,64位上只受系统文件大小的限制。