MongoDB 支持 Capped Collection,一种固定大小的集合,当集合的大小达到指定大小时,
新数据覆盖老数据,MongoDB Replica set 中的 oplog 就是 Capped Collection 类型。

1 查看 oplog 是否是 Capped Collection

 [mongo@redhatB ~]$ mongo 127.0.0.1:
MongoDB shell version: 2.2.
connecting to: 127.0.0.1:/test rs0:PRIMARY> use local;
switched to db local rs0:PRIMARY> show collections;
me
oplog.rs
replset.minvalid
slaves
system.indexes
system.replset rs0:PRIMARY> db.oplog.rs.isCapped();
true

备注:通过 db.collection.isCapped() 命令可以查看一个集合是否是 Capped Collection 。
  
  
   Capped Collection 具有以下特性,在使用的时候需要注意:
  
   1 不可以对 Capped Collection 进行分片。
  
   2 在 2.2 版本以后,创建的Capped Collection 默认在  _id 字段上创建索引,而在 2.2 版本或以前没有。
  
   3 在 Capped Collection 插入文档后可以进行更新(update)操作,当更新不能导致原来文档占用
       空间增长,否则更新失败。
    
   4 不可以对 capped collection 执行删除文档操作,但可以删除整个集合。
   
       接下来会测试其中的部分特性。

2 创建 Capped Collection

 rs0:PRIMARY> db.createCollection("mycoll1",{capped:true,size:});
{ "ok" : }

备注:通过 db.createCollection 命令创建 Capped Collection 集合,创建时必须指定
             集合大小,用于预先分配空间。

3 查看一个集合是否是 Capped Collection
 
   可以通过以下两种方法查看一个集合是否是 Capped Collection 。

 rs0:PRIMARY> db.mycoll1.isCapped();
true rs0:PRIMARY> db.mycoll1.stats();
{
"ns" : "test.mycoll1",
"count" : ,
"size" : ,
"storageSize" : ,
"numExtents" : ,
"nindexes" : ,
"lastExtentSize" : ,
"paddingFactor" : ,
"systemFlags" : ,
"userFlags" : ,
"totalIndexSize" : ,
"indexSizes" : {
"_id_" :
},
"capped" : true,
"max" : ,
"ok" :
}

备注:"capped" 属性为 true 表示是 Capped Collection 。

4 测试:插入记录

 rs0:PRIMARY>  for (var i = ; i <= ; i++) db.mycoll1.save({id : i, name : 'francs'});

rs0:PRIMARY> db.mycoll1.find().count();

rs0:PRIMARY> db.mycoll1.find();
{ "_id" : ObjectId("50b811cf68b1911e7096db7f"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db80"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db81"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db82"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db83"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db84"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db85"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db86"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db87"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db88"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db89"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db8a"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db8b"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db8c"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db8d"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db8e"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db8f"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db90"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db91"), "id" : , "name" : "francs" }
{ "_id" : ObjectId("50b811cf68b1911e7096db92"), "id" : , "name" : "francs" }
Type "it" for more

备注:由于限制了集合大小不小,目标插入 10000 条,结果只插入了 56 条数据,并且老数据被新数据
             覆盖。另外不可以删除 Capped Collection 的文档,下面测试下。
 
5  测试: 删除 capped collection 中的文档

 rs0:PRIMARY> db.mycoll1.remove({id:});
canot remove from a capped collection

备注:删除文档时抛出异常。

6  测试:更新 capped collection 中的文档

 rs0:PRIMARY> db.mycoll1.find({id:});
{ "_id" : ObjectId("50b811cf68b1911e7096db8a"), "id" : , "name" : "francs" } rs0:PRIMARY> db.mycoll1.update({id:},{$set:{name:'aaa_francs'}});
failing update: objects in a capped ns cannot grow rs0:PRIMARY> db.mycoll1.update({id:},{$set:{name:'bbb'}}); rs0:PRIMARY> db.mycoll1.find({id:});
{ "_id" : ObjectId("50b811cf68b1911e7096db8a"), "id" : , "name" : "bbb" }

备注:这里正好验证了特性3,更新后的值不能超过原有空间,否则更新失败。

MongoDB 之 Capped Collection的更多相关文章

  1. 基于mongoDB的capped collection的性能优化

    MonitorLogging改造(消息接入) 改造前架构: 可以看出原来的流程中,大量业务分析,业务接入耦合在web服务层.大量操作,导致线程线性的挂起线程. 改造后: 将业务通讯抽象成为Monito ...

  2. mongodb的capped Collection集合

    db.createCollection(name, {capped: true, autoIndexId: true, size: 1000, max :100} ) name:集合的名字 cappe ...

  3. MongoDB整理笔记のCapped Collection

    1.简单介绍 capped collections 是性能出色的有着固定大小的集合,以LRU(Least Recently Used 最近最少使用)规则和插入顺序进行age-out(老化移出)处理,自 ...

  4. 关于MongoDB 固定集合(capped collection)的知识梳理

    一 . 什么是固定集合 MongoDB中有一种特殊类型的集合,值得我们特别留意,那就是固定集合(capped collection). 固定集合可以声明collection的容量大小,其行为类似于循环 ...

  5. MongoDB固定集合(capped collection)

    一 . 什么是固定集合 MongoDB中有一种特殊类型的集合,值得我们特别留意,那就是固定集合(capped collection). 固定集合可以声明collection的容量大小,其行为类似于循环 ...

  6. mongoDB 固定集合(capped collection)

    固定集合(Capped Collection)是一种尺寸固定的“循环”集合,可提供高效的创建.读取.删除等操作.这里所指的“循环”的意思是,当分配给集合的文件尺寸耗尽时,就会自动开始删除最初的文档,不 ...

  7. Mongodb Capped Collection集合

    MongoDB 固定集合(Capped Collections)是性能出色且有着固定大小的集合,对于大小固定,我们可以想象其就像一个环形队列,当集合空间用完后,再插入的元素就会覆盖最初始的头部的元素! ...

  8. Mongodb学习笔记(二)Capped Collection固定集合

    一.Capped Collection固定集合 简单介绍 capped collections是性能出色的有着固定大小的集合(定容集合),以LRU(Least Recently Used最近最少使用) ...

  9. MongoDB-固定集合 capped collection 操作 介绍

    固定集合:capped collection 是性能出色的固定大小的集合,以LRU算法淘汰记录,自助维护集合中的对象的插入顺序,创建时预先制定大小,空间使用完,心对象取代旧的对象,保持最新的数据. 可 ...

随机推荐

  1. 无法将 lambda 表达式 转换为类型“System.Delegate”,因为它不是委托类型

    今天写winform的时候遇到一个问题,提示: 无法将 lambda 表达式 转换为类型“System.Delegate”,因为它不是委托类型, 主要是为了在子线程中更新UI线程,在wpf中同样的写法 ...

  2. Java API学习(一) ArrayList源码学习

    ArrayList在平常用的还挺多的,用起来十分舒服,顺手.这里来学习一下它的源码. 类定义 下面是类的定义: public class ArrayList<E> extends Abst ...

  3. if语句和switch语句

    1.基本写法 if if(逻辑表达式){语句:}else if{语句:else{语句:} switch switch(变量){case 常量值:语句:break:default:语句:} 2.举例 i ...

  4. linq to sql之组装where条件下的'或'语句

    之前遇到过类似的需求,即前台传入几个过滤条件,后台动态组装where. 例如,前台传入name='张三',age=10, 其余的字段,类似email,QQ之类的本次查询时不做过滤. 用linq to ...

  5. 深入理解OSGI:Java模块化之路

    简介 Java可能是近20年来最成功的开发技术,因其具备通用性.高效性.平台移植性和安全性而成为不同硬件平台理想的开发工具.从笔记本电脑到数据中心,从游戏控制台到科学超级计算机,从手机到互联网,Jav ...

  6. PHP产生随机数

    PHP生成随机字符串包括大小写字母 PHP生成随机字符串包括大小写字母,这里介绍两种方法: 第一种:利用字符串函数操作 ? <?php     /**      *@blog <www.p ...

  7. struts2+hibernate(分页实现)

    //Dao类中实现了list集合和pagetotal方法 package zjf.strhib.Dao; import java.util.ArrayList; import java.util.Li ...

  8. Quick solution to java.lang.NoClassDefFoundError: org/openqa/selenium/HasInputDevices error

    In case if you face this problem, one of the possible solutions that will work for you is to make su ...

  9. C#的publisher与subscriber,事件发布者与订阅者

    说明:示例借鉴自这里,但原版很不友好,于是修改了下,一目了然. 直接上代码: using System; using System.Collections.Generic; using System. ...

  10. perl学习-运算符添加引号

    这个比较有意思,在其它语言中好像没有特别提到 Perl 引号运算符如下表所示. 运算符描述实例 q{ }为字符串添加单引号q{abcd} 结果为 'abcd' qq{ }为字符串添加双引号qq{abc ...