3

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:

  • /var/lib/mongodb
  • /var/lib/mongodb/journal
  • /var/log/mongodb

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.)

2 Answers2

2

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.

Muhammad Faizan
  • 1,709
  • 1
  • 15
  • 37
Marco Talento
  • 2,335
  • 2
  • 19
  • 31
2

I will explain in simple words and try to be brief:

Collection.remove(query)

Anything that you want to remove from a collection based on a query ({}). remember this? It is in your code. This actually is called query, currently empty object in query {} means that it should match all objects. Now why it doesn't free up all the space, because it allocates memory for read and write operations and reserves it for future use. So your data would always be less than space acquired by MongoDB on physical memory.

It removes data only from 1 collection. in your case you only have one collection. To learn how it is working in a little more depth you can follow this link

Db.dropDatabase()

It removes everything from database, your index information, your collection based configuration, everything within the memory, since there is no configuration nor any collection. Your space is free to use.

It removes all the data from all collection and all the meta data of all collections. In fact it removes the database itself. In your case, you can rely on db.dropDatabase() for future usage.

see what effect it produce here

Community
  • 1
  • 1
Muhammad Faizan
  • 1,709
  • 1
  • 15
  • 37
  • Thanks to both of you for your answers and links! Both answered my question, so I accepted the first answer to come in. And I up-voted both of them, but don't have enough reputation for it to have "stuck". Still - thanks! :) – SomeRandomFinn Apr 10 '18 at 12:21
  • Thank You for kind note, please comeback to vote up. It took me longer to answer because I was trying to write more clearly. Well glad I was of help.. Hey if you get reputation later, comeback to vote up may be :D – Muhammad Faizan Apr 10 '18 at 12:29
  • Will do :) I got a notification that my up-vote had been registered, even though it wasn't made visible on the page. So hopefully there's some benefit to you. And should I make my way up in the reputation game, I'll be sure to come back ^^ – SomeRandomFinn Apr 10 '18 at 12:41