I have a sensor hooked up to a Raspi, gathering data using Mongo. Every few days I copy the database (using mongodump) and then delete the Mongo files and restart the sensor.
I have had some trouble deleting/empytying Mongo, and would like to understand what I did wrong.
Initially, I used the commands:
use [database]
db.[nameOfOnlyCollection].remove({})
to empty the database (it only has one collection). Using the command db.[nameOfOnlyCollection].count() I could verify that the collection was empty.
However, even after this, Mongo was still taking lots of space on the Raspi. More specifically these (seemingly Mongo-related) examples took up several gigs of space:
I then found an answer recommending the command db.dropDatabase() to delete all entries in a database.
I now run both db.[nameOfCollection].remove({}) and db.dropDatabase() and this has fixed the problem. Meaning, Mongo no longer takes up gigs worth of space. However, I'd like to understand what's going on.
What is the difference between the commands remove and dropDatabase? When should one use one, when the other? (I'm running both - is that just silly?) Also, does anyone know why Mongo was still taking up lots of space even after running remove?
Any links to relevant reading would be appreciated. (But preferably readings on a "for Dummies" -level. To say I'm a Mongo noob would be an insult to actual Mongo noobs.)
The main difference is that db.remove({}) removes all documents but your collection still have metadata like indexes. The dropDatabase remove literally the entire collection including metadata.
If you are recreating collection indexes or you don't have it please use dropDatabase it's much faster.
Check this link to understand db.collection.drop() vs db.dropDatabase()
If you are into learning MongoDb I recommend reading this book. It also explains your use case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With