Victor
Victor

Reputation: 17077

MongoDB set maximum size limit on data files

I downloaded MongoDb on my local desktop. Since I have other important stuff on this machine, I need to make sure MongoDB data files do not occupy more than 10GB of disk storage at any time. If it exceeds 10GB, I expect an error message while inserting new documents.

Is there a way to set this max size on disk via the config file?

Upvotes: 1

Views: 956

Answers (1)

Stennie
Stennie

Reputation: 65313

As at MongoDB 4.0, there isn't a general configuration option to limit the maximum size on disk for a deployment.

However, there are several ways you could limit storage usage on your desktop:

  • Use a separate partition or storage volume for your MongoDB dbPath

  • Run MongoDB inside a virtual machine or container with a maximum storage allocation

  • Connect to a hosted MongoDB deployment

In general it is a bad idea to let your database server run out of space as this may result in unexpected errors or shutdown depending on the operations that are trying to complete at the point when the server runs out of space. Typically you would want to have a process monitoring storage so you can proactively free some space before the issue becomes critical.

A few space saving tips that might help:

  • Rotate your MongoDB log files regularly to limit storage usage. If you are using a Unix/Linux system you can configure the logrotate utility to rotate and compress logs when they reach a target filesize and subsequently remove archived logs when they reach a certain age.
  • Consider using TTL indexes to automatically remove old data from collections. This can be useful if you have collections with ephemeral data like user sessions that will become stale after an expiry date.
  • Drop unused indexes & collections. The WiredTiger storage engine (default in MongoDB 3.2+) allocates a file per collection and index, so dropping either of those should immediately free up storage space.

Upvotes: 3

Related Questions