开发者

How can I resize a mongodb capped collection without losing data?

How can 开发者_如何学JAVAI resize a mongodb capped collection without losing data?

Is there a command for that, or could somebody provide a script?


This is what I do to resize capped collections:

db.runCommand({"convertToCapped": "log", size: 1000000000});

I already have a Capped Collection named "log". So I just run the "convertToCapped" on it again, specifying a new size. I've not tried it on reducing the size of the collection. That may be something that you'd need to use Scott Hernandez's version on. But this works for increasing the size of your capped collections without losing any data or your indexes.


EDIT: @JMichal is correct. Data is preserved, but indexes are not and will need to be recreated.


You basically need to create a new capped collection and copy the docs to it. This can be done very easily in the javascript (shell), or your language of choice.

db.createCollection("new", {capped:true, size:1073741824}); /* size in bytes */
db.old.find().forEach(function (d) {db.new.insert(d)});
db.old.renameCollection("bak", true);
db.new.renameCollection("old", true);

Note: Just make sure nobody is inserting/updating the old collection when you switch. If you run that code in a db.eval("....") it will lock the server while it runs.

There is a feature request for increasing the size of an existing collection: http://jira.mongodb.org/browse/SERVER-1864


https://www.mongodb.com/docs/manual/core/capped-collections/#change-a-capped-collection-s-size

New in version 6.0:

db.runCommand( { collMod: "log", cappedSize: 100000 } )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜