笔记-capped collection
1. collection
1.1. 简介
集合分为固定与非固定collection,capped collection
1.1.1. capped collections
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.
Capped collections 是固定大小的collection。它支持高速吞吐,这是基于插入顺序实现的。它的工作原理类似于循环缓冲区,一旦接近预定的存储空间限制,它会覆盖集合中最老的文档。
1.2. CURD
1.2.1. create
capped colletions 需要显示创建;使用createcollection方法
db.createCollection(<name>, { capped: <boolean>,
autoIndexId: <boolean>,
size: <number>,
max: <number>,
storageEngine: <document>,
validator: <document>,
validationLevel: <string>,
validationAction: <string>,
indexOptionDefaults: <document>,
viewOn: <string>,
pipeline: <pipeline>,
collation: <document>,
writeConcern: <document>} )
参数释义:
capped:
size:文档空间限制,单位是bytes;只对capped collection生效
max:用于限制文档数,使用时需注意与size的配合。
下面是一些案例:
db.createCollection( "log", { capped: true, size: 100000 } )
db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )
1.2.2. Updates
If you plan to update documents in a capped collection, create an index so that these update operations do not require a collection scan.
1.2.3. delete
You cannot delete documents from a capped collection. To remove all documents from a collection, use the drop() method to drop the collection and recreate the capped collection.
capped collection不支持删除文档操作,清除所有文档的方法是drop()集合然后重新创建。
1.2.4. document size
如果更新或替换操作会导致文档大小改变,操作会失败。
If an update or a replacement operation changes the document size, the operation will fail.
1.3. 其它操作
检查是否为capped集合
db.collection.isCapped()
转换普通集合为限制集合
db.runCommand({"convertToCapped": "mycoll", size: 100000});