Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo DB collection size not changed after removal of fields from document

To reduce my existing collection size, I have removed some unwanted fields from the documents from the collection. After and before I ran the collection stats to check the size, but it never changed. I am missing some thing (like update anything) to reflect the reduced size in my stats, Please advice me. I am running this in my local PC, not having any other nodes. Thank you.

like image 360
Prabhakaran Avatar asked Feb 25 '16 07:02

Prabhakaran


2 Answers

This is correct behavior. The only time mongo releases disk space is when you drop a database or do a repair. (see here)

prettier explanation here

Basically, mongodb keeps any space it has allocated unless and until you drop a database or do a repair. It does this because adding space is not efficient, it is time-consuming. So once it has it, it keeps it and uses the blank space created until there is none left and then it gets more.

like image 151
JohnGalt Avatar answered Nov 03 '22 01:11

JohnGalt


As mentioned by Daniel F in a comment on the question, this worked for me in the mongo shell:

use your-database-name
db.runCommand({ compact: "your-collection-name" })
like image 2
joe Avatar answered Nov 03 '22 02:11

joe