Overview

MongoDB has very similar features with RDBMS database. Like any other DB, it can be shutdown and started up for example:

Basic Management

Start/Stop

You can start MongoDB from a file as follows:

Start

Where the script contains:

startMongoDB

and the contents of the configuration file is:

mongo.conf

The database can also be shutdown as follows:

Stop

or you can do it manually using

Set up Terminal Shell

The shell can be set to more readable manner as follows:

Change storage engine

From 4.2 the: MMAPv1 is depricated, so if you are thinking of upgrading to 4.2, we have to change the storage engine. The engine isn't easy though. In nutshell we should:

Perform export

Create new directory for that storage engine (mongo should have read/write).

Edit the mongo configuration file to use the new folder and storage engine.

Restart the mongo with that storage engine.

Restore the export.

Perform export

The export can be done wherever you can, in our case we will create a new directory for that backup:

The export is done very easy:

Modify the mongo config file

We have to modify the config file so it will point to the new folder and with the correct engine:

P.S. Disable the authentication for now, since it will be problem later:

Restart the mongod

Feel free to restart the mongo however you want :)

Restore the export

If the authentication isn't disabled, that will create a problem since in the config file the authentication is enabled, but the user (which is in the admin database in our case) cannot be used. So ensure the authentication is disabled:

Verify the data

We can verify the data, for some reason mongo was showing me 0000, even though there was data in the databases:

Compact Data

Data in Mongo Database can easily become fragmented. Especially with frequent additions and removals. Let's check one example:

On our example, this collection “file.chunks” is 148870322967 bytes in data but it occupies 394018263040 on disk level. In other words the data is more than 100% fragmented.

In order to defremant a collection we have two options, depending on the storage engine:

MMAPv1 : Repair a whole database or directory location

WiredTiger: Compact Collection.

Let's see how this is done in each of them:

MMAPv1

If your storage engine is MMAPv1, this is your way forward. The repairDatabase command is used for checking and repairing errors and inconsistencies in your data. It performs a rewrite of your data, freeing up any unused disk space along with it. Like compact, it will block all other operations on your database. Running repairDatabase can take a lot of time depending on the amount of data in your db, and it will also completely remove any corrupted data it finds.

Linux Shell

Mongo Shell

or

RepairDatabase needs free space equivalent to the data in your database and an additional 2GB more. It can be run either from the system shell or from within the mongo shell. Depending on the amount of data you have, it may be necessary to assign a sperate volume for this using the –repairpath option.

Wired Tiger

he compact command works at the collection level, so each collection in your database will have to be compacted one by one. This completely rewrites the data and indexes to remove fragmentation. In addition, if your storage engine is WiredTiger, the compact command will also release unused disk space back to the system. You're out of luck if your storage engine is the older MMAPv1 though; it will still rewrite the collection, but it will not release the unused disk space. Running the compact command places a block on all other operations at the database level, so you have to plan for some downtime.

Example

So let's check it in action with WiredTiger:

WoW, we saved more than half of the space :)