Does MongoDB reuse space after deleting\shrinking documents?

59 views
Skip to first unread message

Bryan Migliorisi

unread,
Aug 8, 2011, 4:05:13 PM8/8/11
to mongod...@googlegroups.com
I am doing some work around metrics and analytics and my plan is to use a single document for measuring unique metrics to a given object in our system.

The do this, I was planning on using the $addToSet operator to effectively push unique IP address or member IDs into a subdocument.  

My application would read this array and calculate the unique hits for a given object on a given date.  To prevent unnecessary bloat, I would archive this document after the last possible date for it to be modified (i.e. if this document represented metrics for the month of June, I would archive it on July 1st).  The archival process would be simple:  Read the document, calculate the number of unique hits, update the document by removing the unique_hits array subdocument and writing a new value that simple contains the integer sum value.

This part I know will work, but my concern is that when the document grows, it will need to be moved around disk\memory due to the nature of how MongoDB works, and when I archive it and basically rewrite the document by removing the large subdocument\array and replacing it with a simple integer, the document will once again leave behind empty space.

I know I can do a repairDatabase command or use compact command in 1.9+ but I am wondering if MongoDB will automatically attempt to reuse that space that is left behind after the document grows or shrinks.  I would like to avoid using any low level compaction commands because of the write lock.

Joshua Kehn

unread,
Aug 8, 2011, 4:09:30 PM8/8/11
to mongod...@googlegroups.com
Last I checked, no it does not release the space for system use. It may be planned for future work though. 

Regards,

-Josh
____________________________________
Joshua Kehn | Josh...@gmail.com

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/s4-bT01MjHkJ.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Bryan Migliorisi

unread,
Aug 8, 2011, 4:18:44 PM8/8/11
to mongod...@googlegroups.com
Thanks Josh, but I am not even concerned about releasing it for system use.  

MongoDB pre-allocates disk space (in predictable sizes) during normal usage.  When it does this, it should always know where the free space exists in its own data files.  I would imagine that it would know the same information whether the data file was created empty or if data was removed, leaving empty pockets in that data file.

--Bryan Migliorisi

sridhar

unread,
Aug 8, 2011, 6:13:48 PM8/8/11
to mongodb-user
MongoDB maintains information about the free space in datafiles
(caused by deletes etc.) and reuses them.
http://www.mongodb.org/display/DOCS/Excessive+Disk+Space#ExcessiveDiskSpace-DeletedSpace.
You are correct in that it never releases the space to the OS itself.

Bryan Migliorisi

unread,
Aug 8, 2011, 10:28:27 PM8/8/11
to mongod...@googlegroups.com
Excellent.  Thanks for the reference too.
Reply all
Reply to author
Forward
0 new messages