【mongodb之固定集合】

创建固定集合

有点类似数据结构中的循坏队列,先进先出方式。满队列删除原来的元素。

MongoDB 固定集合(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.

Capped Collection(固定集合)简单介绍:

Cappend collection是性能出色的有着固定大小的集合,以LUR(Least Recently Used最近最少使用)规则和插入顺序进行age-out(老化移出)处理,自动维护集合中对象的插入顺序,在创建时要预先指定大小,如果空间用完,新添加的对象将会取代集合最旧的对像,永远保持最新的数据

功能特点:

可以插入及更新,但更新不能超过collection的大小,否则更新失败,不允许删除,但是可以调用drop()删除集合中的所有行,但是drop后需要显示的重建集合,在32位机目前一个capped collection的最大值约为482.5M,64位上只受系统文件大小的限制

属性及用法:

属性1:对固定集合进行插入速度比较快

属性2:按照插入顺序的查询速度比较快

属性3:能够在插入最新数据时,淘汰最早的数据

用法1:储存日志信息

用法2:缓存一些少量的文档

创建固定集合

不像普通集合,固定集合需要显式的创建使用

createCollection命令来创建

db.createCollection('my_collection', {capped:true, size:10000});

创建一个集合为"my_collection"的固定庥合,大小为10000字节,还可以限定文档个数,加上Max 100属性

注意: 指定的文档上限,必须指定大小,文档限制是容量没满时进行淘汰,要是满了,就根据容量限制进行淘汰

db.c1.stats(); //查看集合的详细信息,capped是否为真表示为固定集合

转换为固定集合

普通集合可以通过 convertToCapped将普通集合转为固定集合

db.runCommand({convertToCapped:”test_2”,size:10,max:3})

但是固定集合的特点是: 

不能进行分区,因为它本身就是不是针对大数据集合的,所以,也许这个时候可以考虑使用TTL索引。

猜你喜欢

转载自gaojingsong.iteye.com/blog/2382812