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. js生成验证码并且验证

    <html> <head> <title>验证码</title> <style type="text/css"> #co ...

  2. ILOG JRules 和 WebSphere Process Server 集成概述

    ILOG JRules 和 WebSphere Process Server 集成概述 简介 业务流程管理(Business Process Management,BPM)和业务规则管理系统(Busi ...

  3. linux下修改tomcat使用的jdk版本

    遇到一种情况,就是linux上好像掉文件了,JDK的目录下没有了,具体问题还不清楚,不过要赶紧修复,不能影响其他程序的运行. 结构重新安装了JDK,tomcat还是启动失败,看l启动日志发现没找到还是 ...

  4. # Writing your-first Django-app-part 5 -test

    确认bug 写test测试暴露bug 修复bug 更多测试例子 测试一个view The Django test client测试客户端. 提升DemoAppPoll/views.py 测试我们的vi ...

  5. Linxu磁盘分区

    http://vbird.dic.ksu.edu.tw/linux_basic/0130designlinux.php#hardware_know(好文章) http://blog.chinaunix ...

  6. 【oneday_onepage】——How to stretch the life of your SSD storage

    How to stretch the life of your SSD storage July 18, 2013, 9:06 PM — Once a PC enthusiast's dream st ...

  7. java jdbc preparedstatement 分析

    https://blog.csdn.net/xiong9999/article/details/54137326

  8. 深入浅出学习Hibernate框架(二):JDBC基础操作

    上篇博客<深入浅出学习Hibernate框架(一):从实例入手初识Hibernate框架>简单介绍了一下Hibernate框架,并且举了一个实例来了解Hibernate.这篇博客将介绍JD ...

  9. javascript 学习记录

    关于牛B的Jquery源头 (function(){ //这里省略jQuery所有实现 })(); :无论你怎么去定义你的函数 JS解释器都会把它翻译成一个 Function对象 :那什么是Funct ...

  10. 多变量频率统计——r

    例如有X1,X2,..,Xn个变量,我需要对每一个变量进行频次统计,如果一个一个求解的话非常麻烦,如table(X1), table(X2), ... ,table(Xn).有没有简单的语句一次性求解 ...