Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3618894
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:48:37+00:00 2026-05-18T22:48:37+00:00

The mongodb document says that To compact this space, run db.repairDatabase() from the mongo

  • 0

The mongodb document says that

To compact this space, run db.repairDatabase() from the mongo shell (note this operation will block and is slow).

in http://www.mongodb.org/display/DOCS/Excessive+Disk+Space

I wonder how to make the mongodb free deleted disk space automatically ?

p.s. We stored many downloading task in mongodb, up to 20GB, and finished these in half an hour.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:48:38+00:00Added an answer on May 18, 2026 at 10:48 pm

    In general if you don’t need to shrink your datafiles you shouldn’t shrink them at all. This is because “growing” your datafiles on disk is a fairly expensive operation and the more space that MongoDB can allocate in datafiles the less fragmentation you will have.

    So, you should try to provide as much disk-space as possible for the database.

    However if you must shrink the database you should keep two things in mind.

    1. MongoDB grows it’s data files by
      doubling so the datafiles may be
      64MB, then 128MB, etc up to 2GB (at
      which point it stops doubling to
      keep files until 2GB.)

    2. As with most any database … to
      do operations like shrinking you’ll
      need to schedule a separate job to
      do so, there is no “autoshrink” in
      MongoDB. In fact of the major noSQL databases
      (hate that name) only Riak
      will autoshrink. So, you’ll need to
      create a job using your OS’s
      scheduler to run a shrink. You could use an bash script, or have a job run a php script, etc.

    Serverside Javascript

    You can use server side Javascript to do the shrink and run that JS via mongo’s shell on a regular bases via a job (like cron or the windows scheduling service) …

    Assuming a collection called foo you would save the javascript below into a file called bar.js and run …

    $ mongo foo bar.js
    

    The javascript file would look something like …

    // Get a the current collection size.
    var storage = db.foo.storageSize();
    var total = db.foo.totalSize();
    
    print('Storage Size: ' + tojson(storage));
    
    print('TotalSize: ' + tojson(total));
    
    print('-----------------------');
    print('Running db.repairDatabase()');
    print('-----------------------');
    
    // Run repair
    db.repairDatabase()
    
    // Get new collection sizes.
    var storage_a = db.foo.storageSize();
    var total_a = db.foo.totalSize();
    
    print('Storage Size: ' + tojson(storage_a));
    print('TotalSize: ' + tojson(total_a));
    

    This will run and return something like …

    MongoDB shell version: 1.6.4
    connecting to: foo
    Storage Size: 51351
    TotalSize: 79152
    -----------------------
    Running db.repairDatabase()
    -----------------------
    Storage Size: 40960
    TotalSize: 65153
    

    Run this on a schedule (during none peak hours) and you are good to go.

    Capped Collections

    However there is one other option, capped collections.

    Capped collections are fixed sized
    collections that have a very high
    performance auto-FIFO age-out feature
    (age out is based on insertion order).
    They are a bit like the “RRD” concept
    if you are familiar with that.

    In addition, capped collections
    automatically, with high performance,
    maintain insertion order for the
    objects in the collection; this is
    very powerful for certain use cases
    such as logging.

    Basically you can limit the size of (or number of documents in ) a collection to say .. 20GB and once that limit is reached MongoDB will start to throw out the oldest records and replace them with newer entries as they come in.

    This is a great way to keep a large amount of data, discarding the older data as time goes by and keeping the same amount of disk-space used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 extension methods that convert a MongoDB document to an entity/object. public
In MongoDB, I have a document with a field called ClockInTime that was imported
The limitation of one document in MongoDB is 4Mo. I have some documents really
Im looking to use a document database such as MongoDB but looking through the
Are there any document databases and/or key-value stores (something like Project Voldemort, MongoDB, etc.)
For make a document managment system, I'm looking at document stores like MongoDB, but
Using MongoDB C# driver ( http://github.com/samus/mongodb-csharp ), seems that I'm unable to get the
I have a class Document (in a mongodb collection) and persist various sub classes
I've looked at MongoDB/norm (but want a general document-oriented store answer). It seems really
By default MongoDB creates index on _id key in document. But when I ensure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.