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.