Mongodb 简明教程

MongoDB - Capped Collections

Capped collections 是固定大小的循环集合,它遵循插入顺序来支持高性能创建、读取和删除操作。循环是指当分配给集合的固定大小用尽时,它将开始删除集合中最旧的文档,而无需提供任何显式命令。

Capped collections are fixed-size circular collections that follow the insertion order to support high performance for create, read, and delete operations. By circular, it means that when the fixed size allocated to the collection is exhausted, it will start deleting the oldest document in the collection without providing any explicit commands.

固定大小集合限制了文档的更新,如果更新会导致文档大小增加。由于固定大小集合按磁盘存储的顺序存储文档,因此它确保文档大小不会增加磁盘分配的大小。固定大小集合最适合存储日志信息、缓存数据或任何其他高容量数据。

Capped collections restrict updates to the documents if the update results in increased document size. Since capped collections store documents in the order of the disk storage, it ensures that the document size does not increase the size allocated on the disk. Capped collections are best for storing log information, cache data, or any other high volume data.

Creating Capped Collection

要创建固定大小集合,我们使用常规 createCollection 命令,但使用 capped 选项为 true ,并指定集合的最大大小(以字节为单位)。

To create a capped collection, we use the normal createCollection command but with capped option as true and specifying the maximum size of collection in bytes.

>db.createCollection("cappedLogCollection",{capped:true,size:10000})

除了集合大小之外,我们还可以使用 max 参数限制集合中的文档数量:

In addition to collection size, we can also limit the number of documents in the collection using the max parameter −

>db.createCollection("cappedLogCollection",{capped:true,size:10000,max:1000})

如果你想检查一个集合是否固定大小, hãy使用以下 isCapped 命令:

If you want to check whether a collection is capped or not, use the following isCapped command −

>db.cappedLogCollection.isCapped()

如果有一个你正在计划转换为固定大小的现有集合,你可以使用以下代码执行:

If there is an existing collection which you are planning to convert to capped, you can do it with the following code −

>db.runCommand({"convertToCapped":"posts",size:10000})

此代码会将我们现有的集合 posts 转换为固定大小集合。

This code would convert our existing collection posts to a capped collection.

Querying Capped Collection

默认情况下,对固定大小集合执行的 find 查询将按插入顺序显示结果。但是,如果你希望反向检索文档,请使用 sort 命令,如下代码所示:

By default, a find query on a capped collection will display results in insertion order. But if you want the documents to be retrieved in reverse order, use the sort command as shown in the following code −

>db.cappedLogCollection.find().sort({$natural:-1})

关于固定大小集合还有一些其他值得了解的重要事项:

There are few other important points regarding capped collections worth knowing −

  1. We cannot delete documents from a capped collection.

  2. There are no default indexes present in a capped collection, not even on _id field.

  3. While inserting a new document, MongoDB does not have to actually look for a place to accommodate new document on the disk. It can blindly insert the new document at the tail of the collection. This makes insert operations in capped collections very fast.

  4. Similarly, while reading documents MongoDB returns the documents in the same order as present on disk. This makes the read operation very fast.