LoginSignup
2
2

More than 5 years have passed since last update.

MongoDBでConvertToCappedしてmaxがおかしな事になっても大丈夫らしい

Last updated at Posted at 2015-06-10

とりあえずやってみる。

db.runCommand({"convertToCapped": "hoge", size: 100});

> db.hoge.stats()
{
    "ns" : "xxx.hoge",
    "count" : 1,
    "size" : 24,
    "avgObjSize" : 24,
    "storageSize" : 4096,
    "numExtents" : 1,
    "nindexes" : 1,
    "lastExtentSize" : 4096,
    "paddingFactor" : 1,
    "systemFlags" : 1,
    "userFlags" : 0,
    "totalIndexSize" : 8176,
    "indexSizes" : {
        "_id_" : 8176
    },
    "capped"l
    "max" : NumberLong("9223372036854775807"),
    "ok" : 1
}

max: : NumberLong("9223372036854775807")
ファッ!?

documentをよく読む。
http://docs.mongodb.org/manual/core/capped-collections/

Additionally, you may also specify a maximum number of documents for the collection using the max field as in the following document:

という事で size はbyte数、maxはドキュメントの数のようです。

db.runCommand({"convertToCapped": "hoge", size: 100, max: 100});

にしても max変化せず。

によるとcreateCollectionのときしか効かないようで…紛らわしいですmongoさん。

db.createCollection("hoge", {capped: true, size: 40960})

で以下のようにstorageSizeが指定したものになるので初期に指定sizeのStorage取っちゃってcapped:trueだと増えないんですね。

> db.hoge.stats()
{
    "ns" : "xxx.hoge",
    "count" : 0,
    "size" : 0,
    "storageSize" : 40960,
    "numExtents" : 1,
    "nindexes" : 1,
    "lastExtentSize" : 40960,
    "paddingFactor" : 1,
    "systemFlags" : 1,
    "userFlags" : 0,
    "totalIndexSize" : 8176,
    "indexSizes" : {
        "_id_" : 8176
    },
    "capped" : true,
    "max" : NumberLong("9223372036854775807"),
    "ok" : 1
}

まとめ

  • size指定したstorageを確保し、cappedされていればそれ以上増えない
  • maxを指定してあればそのドキュメント数を上限とする。
  • maxはcreateCollectionのに指定可能で後から付ける方法は無い

convertToCappedが内部的に使ってる cloneCollectionAsCapped ってまあcreateしてコピーしてるはずなのに、どうして中途半端に制限が存在するのか…
コピー時に上限に達したときの処理を作るのがめんどくさかったんでしょうか…書いててちょっと気持ちがわかりました。

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2