- MongoDB官方文档中文版
- MongoDB中文手册说明
- MongoDB简介
- 安装 MongoDB
- The mongo Shell
- MongoDB CRUD 操作
- 聚合
- 数据模型
- 事务
- 索引
- 安全
- 安全检查列表
- 启用访问控制
- 身份验证
- 基于角色的访问控制
- TLS / SSL(传输加密)
- 静态加密
- 客户端字段级加密
- 审计
- 网络和配置强化
- 实现字段级别修订
- 安全参考
- 附录
- 变更流
- 复制
- 分片
- 分片键
- 哈希分片
- 范围分片
- 区
- 管理分片区
- 按位置细分数据
- 用于更改SLA或SLO的分层硬件
- 按应用或客户细分数据
- 仅插入工作负载的分布式本地写入
- 管理分片区
- 使用块进行数据分区
- 在分片集群中拆分数据块
- 管理
- 存储
- 存储引擎
- 日志记录
- 管理日志记录
- GridFS
- FAQ:MongoDB 存储
- 存储引擎
- 参考
- 运算符
- 查询与映射运算符
- 更新运算符
- 聚合管道阶段
- 聚合管道操作符
- $abs (aggregation)
- $acos (aggregation)
- $acosh (aggregation)
- $add (aggregation)
- $addToSet (aggregation)
- $allElementsTrue (aggregation)
- $and (aggregation)
- $anyElementTrue (aggregation)
- $arrayElemAt (aggregation)
- $arrayToObject (aggregation)
- $asin (aggregation)
- $asinh (aggregation)
- $atan (aggregation)
- $atan2 (aggregation)
- $atanh (aggregation)
- $avg (aggregation)
- $ceil (aggregation)
- $cmp (aggregation)
- $concat (aggregation)
- $concatArrays (aggregation)
- $cond (aggregation)
- $convert (aggregation)
- $cos (aggregation)
- $dateFromParts (aggregation)
- $dateToParts (aggregation)
- $dateFromString (aggregation)
- $literal (aggregation)
- 查询修饰符
- 数据库命令
- 聚合命令
- 地理空间命令
- 查询和写操作命令
- 查询计划缓存命令
- 认证命令
- 用户管理命令
- 角色管理命令
- 复制命令
- 分片命令
- 会话命令
- 管理命令
- 诊断命令
- 免费监控命令
- 系统事件审计命令
- mongo Shell 方法
- 集合方法
- db.collection.aggregate()
- db.collection.bulkWrite()
- db.collection.copyTo()
- db.collection.count()
- db.collection.countDocuments()
- db.collection.estimatedDocumentCount()
- db.collection.createIndex()
- db.collection.createIndexes()
- db.collection.dataSize()
- db.collection.deleteOne()
- db.collection.deleteMany()
- db.collection.distinct()
- db.collection.drop()
- db.collection.dropIndex()
- db.collection.dropIndexes()
- db.collection.ensureIndex()
- db.collection.explain()
- db.collection.find()
- db.collection.findAndModify()
- db.collection.findOne()
- db.collection.findOneAndDelete()
- db.collection.findOneAndReplace()
- db.collection.findOneAndUpdate()
- db.collection.getIndexes()
- db.collection.getShardDistribution()
- db.collection.getShardVersion()
- db.collection.insert()
- db.collection.insertOne()
- db.collection.insertMany()
- db.collection.isCapped()
- db.collection.latencyStats()
- db.collection.mapReduce()
- db.collection.reIndex()
- db.collection.remove()
- db.collection.renameCollection()
- db.collection.replaceOne()
- db.collection.save()
- db.collection.stats()
- db.collection.storageSize()
- db.collection.totalIndexSize()
- db.collection.totalSize()
- db.collection.update()
- db.collection.updateOne()
- db.collection.updateMany()
- db.collection.watch()
- db.collection.validate()
- 词汇表
- 默认的MongoDB端口
- 默认的MongoDB读/写关注
- 服务器会话
- MongoDB驱动
- FAQ
- 联系我们
- 更多资料
- [快学Mongo]
- [Mongo问题讨论区]
- [Mongo 驱动使用手册]
- 本书使用 GitBook 发布
封顶集合
封顶集合
本文索引
概述
封顶集合 capped collection 是固定大小的集合, 支持高吞吐的插入操作和根据插入顺序的查询操作. 封顶集合的工作方式与循环缓冲区 (circular buffers) 类似: 当一个集合填满了被分配的空间, 则通过覆盖最早的文档来为新的文档腾出空间.
参阅 createCollection()
或 create
获取更多创建封顶集合的信息.
表现
顺序插入
封顶集合保证了插入的顺序. 因此, 历史查询不需要索引排序. 没有这种索引开销, 封顶集合可以支持更高的插入吞吐量.
自动删除最早的文档
为了给新的文档腾出空间, 封顶集合会自动删除集合中最早的文档, 不需要定时脚本或者显示的删除操作.
例如, 在 oplog.rs 集合中存储了 replica set 的操作的日志, 该集合就使用的是封顶集合. 除此之外, 还可以考虑以下潜在的用例:
- 存储由高容量 (high-volume) 系统生成的日志信息. 在封顶集合中不用索引插入文档的速度接近直接输出日志到文件系统. 而且, 内置的 先进先出 的属性维护了事件的顺序, 这在管理存储时用得到 (译注: 有些日志存储系统的顺序可能会乱, 如 Elasticsearch).
- 在封顶集合中记性数据缓存 (少量的). 由于缓存是高频读很少写, 因此你需要确保集合 始终 保留在工作区间 (即 RAM 中) 或者 接受一些使用索引带来的写入的成本 (or accept some write penalty for the required index or indexes ).
_id
索引
封顶集合有 _id
字段并且有一个基于 _id
字段的默认索引.
限制与推荐
更新
如果您计划更新封顶集合中的文档, 请创建一个索引, 来避免更新操进行集合扫描.
文档大小
在 3.2 版本中修改.
更新或替换文档大小的操作会失败. (注: 之前的 MMAPv1 可以修改)
文档删除
你不能删除封顶集合中的文档. 要删除集合中的所有文档, 请使用 drop()
方法删除集合, 并重新创建封顶的集合.
分片
你不能对封顶集合进行分片操作.
查询效率
使用自然顺序 (natural ordering) 来有效地检索集合最近插入的元素. 这 (有点) 类似 tail
一个日志文件 (查看他的尾部).
聚合 $out
聚合管道操作符 $out
不能将结果写入封顶集合.
使用
创建封顶集合
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()
方法显式地创建封顶集合, 该过程可以通过 mongo
shell 来帮忙执行 create
命令. 在创建封顶集合时, 您必须预先指定集合的最大容量 (以字节为单位). 其中包括少量的内部空间.
db.createCollection( "log", { capped: true, size: 100000 } )
如果 size
字段小于或等于 4096, 则该集合将具有 4096 字节的容量. 此外, MongoDB 会提升用户所提供给的 size 大小直到其满足 256 的倍数为止.
Additionally, you may also specify a maximum number of documents for the collection using themax
field as in the following document:
db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )
IMPORTANT
The
size
argument is_always_required, even when you specifymax
number of documents. MongoDB will remove older documents if a collection reaches the maximum size limit before it reaches the maximum document count.
SEE
db.createCollection()
andcreate
.
封顶集合查询
If you perform afind()
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, issuefind()
along with thesort()
method with the$natural
parameter set to-1
, as shown in the following example:
db.cappedCollection.find().sort( { $natural: -1 } )
检查集合是否封顶
Use theisCapped()
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 theconvertToCapped
command:
db.runCommand({"convertToCapped": "mycoll", size: 100000});
Thesize
parameter specifies the size of the capped collection in bytes.
WARNING
This command obtains a global write lock and will block other operations until it has completed.
Automatically Remove Data After a Specified Period of Time
As an alternative to 封顶集合, consider MongoDB’sTTL(“time to live”) indexes. As described inExpire 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.
IMPORTANT
TTL indexesare not compatible with 封顶集合.
Tailable Cursor
You can use atailable cursorwith 封顶集合. Similar to the Unixtail-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.
SeeTailable Cursorsfor information on creating a tailable cursor.
原文链接:https://docs.mongodb.com/v4.2/core/capped-collections/
Copyright © 上海锦木信息技术有限公司 all right reserved,由 MongoDB汉化小组 提供技术支持文件修订时间: 2020-10-11 20:53:05