Capped Collection
Last updated
Was this helpful?
Last updated
Was this helpful?
Capped collections 是一种大小固定的集合,他基于插入排序算法,支持高吞吐量的文档插入和文档查询操作。
Capped collections 的工作原理有点像循环缓冲区,一旦它存储的空间满了,新的文档便会覆盖旧的文档。
如果你计划在一个capped集合更新里面的文档,则创建索引可以避免全表扫描
从3.2版本开始, 如果你执行的更新或替换操作改变了capped集合的大小,这个操作将会失败
我们是不能删除capped集合里面的文档的,如果要删除文档,只能把这个capped集合删除并重新创建一个新的
capped 集合不能分片
Use natural ordering to retrieve the most recently inserted elements from the collection efficiently. This is (somewhat) analogous to tail on a log file
The aggregation pipeline operator $out cannot write results to a capped collection.
创建步骤如上
如果 find 方法没有指定查询方式,则默认是正向排序,即返回最早插入的文档
如果要反向查询,则可以用下列方法
db.mycoll.find().sort( { $natural: -1 } )
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.
有关创建的详细信息,参见
该功能跟redis键的过期清理类似,详情参见
See for information on creating a tailable cursor.