How can I resize a mongodb capped collection witho

2019-01-16 18:20发布

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

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

标签: mongodb nosql
2条回答
Anthone
2楼-- · 2019-01-16 18:56

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.

查看更多
淡お忘
3楼-- · 2019-01-16 19:00

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

查看更多
登录 后发表回答