Dave Causey
Dave Causey

Reputation: 14068

MongoDB database growth with only one record

I have a node.js application that saves a single record repeatedly to a mongodb database as in the code sample below. The data record is actually several MB in size, but there is only one record in one collection at any point in time. Still, the database increases in size continuously until the server runs out of disk space.

_db.collection('myCollection', function(err, collection) {
  collection.remove({}, function(err, result) {
    collection.insert(myData);
  });
});

I realize there are better ways to manage this data, and simply repairing the database a couple of times a month will manage the problem, but I was really curious as to why mongodb wouldn't automatically reclaim the space in this situation.

Upvotes: 0

Views: 237

Answers (1)

c0deNinja
c0deNinja

Reputation: 3986

One way to work around this problem is to set a max size on your collection... since it will always only have 1 record. Try something like this:

_db.createCollection("myCollection", {size:100000});

You can read a little bit about MongoDB disk usage here: Excessive Disk Space

Upvotes: 1

Related Questions