mongodb Capped Collections 固定集合

特点

  • 像队列,插入的效率很高,size大小固定(还可以指定max限制文档个数),自动按照插入的顺序返回文档
  • 不能从capped集合中删除文档,只能整个集合一块删除
  • 不能sharding

使用

When creating a capped collection you must specify the maximum size of the collection in bytes, which MongoDB will pre-allocate for the collection. The size of the capped collection includes a small amount of space for internal overhead.

db.createCollection( "log", { capped: true, size: 100000 } )

注意:

If the size field is less than or equal to 4096, then the collection will have a cap of 4096 bytes. Otherwise, MongoDB will raise the provided size to make it an integer multiple of 256.

指定文档个数

db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )

使用natural操作符

可以让返回顺序变为插入的逆序

db.cappedCollection.find().sort( { $natural: -1 } )

Check if a Collection is Capped

db.collection.isCapped()

Convert a Collection to Capped

db.runCommand({"convertToCapped": "mycoll", size: 100000});

Automatically Remove Data After a Specified Period of Time

consider MongoDB’s TTL indexes,
see mongodb manual

posted @ 2016-08-16 12:07  jcuan  阅读(519)  评论(0编辑  收藏  举报