封顶集合

    封顶集合 capped collection 是固定大小的集合, 支持高吞吐的插入操作和根据插入顺序的查询操作. 封顶集合的工作方式与循环缓冲区 (circular buffers) 类似: 当一个集合填满了被分配的空间, 则通过覆盖最早的文档来为新的文档腾出空间.

    参阅 或 create 获取更多创建封顶集合的信息.

    封顶集合保证了插入的顺序. 因此, 历史查询不需要索引排序. 没有这种索引开销, 封顶集合可以支持更高的插入吞吐量.

    自动删除最早的文档

    为了给新的文档腾出空间, 封顶集合会自动删除集合中最早的文档, 不需要定时脚本或者显示的删除操作.

    例如, 在 oplog.rs 集合中存储了 的操作的日志, 该集合就使用的是封顶集合. 除此之外, 还可以考虑以下潜在的用例:

    • 在封顶集合中记性数据缓存 (少量的). 由于缓存是高频读很少写, 因此你需要确保集合 始终 保留在工作区间 (即 RAM 中) 或者 接受一些使用索引带来的写入的成本 (or accept some write penalty for the required index or indexes
      ).

    _id 索引

    封顶集合有 _id 字段并且有一个基于 _id 字段的默认索引.

    更新

    如果您计划更新封顶集合中的文档, 请创建一个索引, 来避免更新操进行集合扫描.

    文档大小

    在 3.2 版本中修改.

    更新或替换文档大小的操作会失败. (注: 之前的 MMAPv1 可以修改)

    你不能删除封顶集合中的文档. 要删除集合中的所有文档, 请使用 方法删除集合, 并重新创建封顶的集合.

    分片

    查询效率

    使用自然顺序 (natural ordering) 来有效地检索集合最近插入的元素. 这 (有点) 类似 tail 一个日志文件 (查看他的尾部).

    聚合

    聚合管道操作符 不能将结果写入封顶集合.

    创建封顶集合

    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.

    您必须使用 方法显式地创建封顶集合, 该过程可以通过 mongo shell 来帮忙执行 命令. 在创建封顶集合时, 您必须预先指定集合的最大容量 (以字节为单位). 其中包括少量的内部空间.

    如果 size 字段小于或等于 4096, 则该集合将具有 4096 字节的容量. 此外, MongoDB 会提升用户所提供给的 size 大小直到其满足 256 的倍数为止.

    Additionally, you may also specify a maximum number of documents for the collection using themaxfield as in the following document:

    SEE

    and.

    To retrieve documents in reverse insertion order, issuefind()along with themethod with the$naturalparameter set to-1, as shown in the following example:

    检查集合是否封顶

    Use theisCapped()method to determine if a collection is capped, as follows:

    集合转换为封顶集合

    You can convert a non-capped collection to a capped collection with theconvertToCappedcommand:

    Theparameter specifies the size of the capped collection in bytes.

    Automatically Remove Data After a Specified Period of Time

    As an alternative to 封顶集合, consider MongoDB’sTTL(“time to live”) indexes. As described in, 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.

    Tailable Cursor

    You can use awith 封顶集合. Similar to the Unixtail-fcommand, 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.