zoukankan      html  css  js  c++  java
  • MongoDB 之 Capped Collection

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


    1 查看 oplog 是否是 Capped Collection

     [mongo@redhatB ~]$ mongo 127.0.0.1:27018
    MongoDB shell version: 2.2.1
    connecting to: 127.0.0.1:27018/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:1024}); 
    { "ok" : 1 }

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


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

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

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


    4 测试:插入记录

     rs0:PRIMARY>  for (var i = 1; i <= 10000; i++) db.mycoll1.save({id : i, name : 'francs'});
    
    rs0:PRIMARY> db.mycoll1.find().count();
    56
    
    rs0:PRIMARY> db.mycoll1.find();
    { "_id" : ObjectId("50b811cf68b1911e7096db7f"), "id" : 9945, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db80"), "id" : 9946, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db81"), "id" : 9947, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db82"), "id" : 9948, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db83"), "id" : 9949, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db84"), "id" : 9950, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db85"), "id" : 9951, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db86"), "id" : 9952, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db87"), "id" : 9953, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db88"), "id" : 9954, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db89"), "id" : 9955, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db8a"), "id" : 9956, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db8b"), "id" : 9957, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db8c"), "id" : 9958, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db8d"), "id" : 9959, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db8e"), "id" : 9960, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db8f"), "id" : 9961, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db90"), "id" : 9962, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db91"), "id" : 9963, "name" : "francs" }
    { "_id" : ObjectId("50b811cf68b1911e7096db92"), "id" : 9964, "name" : "francs" }
    Type "it" for more

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

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

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


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

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

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

  • 相关阅读:
    virtualbox mout 错误解决
    Python pip 学习
    tzset()与localtime()
    TSE部署实践
    变量和关系符和JAVA基本类型笔记与常考面试题
    实例:判断客户是否中奖与纸牌互换,计算银行本息
    实例:五位数各位数之和与员工实领工资
    关于变量,JAVA基本数据类型,运算符类型,如何从控制台接收输入的数据
    Java的程序执行过程与编译原理
    CMD控制器常用命令
  • 原文地址:https://www.cnblogs.com/jokerjason/p/7776566.html
Copyright © 2011-2022 走看看