I notice that the size of my mongodb has become smaller after using mongodump and mongorestore to back up and restore the dabase:
> show dbs
iotdb1 0.611GB
iotdb2 0.476GB
The original size is 0.611GB but now is 0.476GB.
Is this normal?
Short Answer:
DB size doesn't matter when you dump & restore. Documents count only matters.
Explanation:
Mongodump will not take the backup of the oplog
so there might be some space difference you can see. Also when you take dump there is also some data holes or bloat
that already present in the system will be removed when you restore them. These are the two main reasons that you feel there is change in the size of the DB after restoring them.
Verification:
You can use the below command to find the list of all documents size before & after on both the DBs:
db.getCollectionNames().forEach(function(collection) { resultCount = db[collection].count(); print("Number of documents for " + collection + ": "+ resultCount); });
Sample Output:
> db.getCollectionNames().forEach(function(collection) { resultCount = db[collection].count(); print("Number of documents for " + collection + ": "+ resultCount); });
Number of documents for credits: 17643
Number of documents for data: 500302
Number of documents for debts: 387
Number of documents for food: 309875
Number of documents for loans: 64926
Number of documents for payroll: 2436
Number of documents for tasks: 11564
Number of documents for salary: 883457
Number of documents for savings: 494
So with the above query you can easily compare the both DBs & get to know the documents number match.
I believe that what happens here is that the backup does not saves everything but that's properly from the MongoDB mongodump command, as you can see in the documentation says: "mongodump excludes the content of the local database in its output.
mongodump output only captures the documents in the database and does not include index data. mongorestore or mongod must then rebuild the indexes after restoring data."
So, If you can try restorint it with mongorestore and everything is normal, then the backup is just simplified.
Here you can read more: https://docs.mongodb.com/manual/reference/program/mongodump/