MongoDB 之 Capped Collection

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,更新后的值不能超过原有空间,否则更新失败。

上一篇:poj 1816 (Trie + dfs)


下一篇:css系列之box-sizing