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:

Storage

The storage and memory can be overviewed as follows:

Using Server Status

Server status will give you some rough overview of how the memory is allocated:

So you can see how much memory your database is storing.

The storage can be checked as follows:

Of course you can investigate it further with:

Get Storage Size for each Colleciton

Get Storage per Collection

Get Storage per Collection in GB

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

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:

Logical Structure

As any database, mongo has objects to store information into, we already discussed them:

Data Objects

Collections

Documents

As you know, a collection can store many documents, which doesn't even need to have the same structure. Imagine a collection, just a laundry bin, in which you put a lot of shirts. These shirts, don't even have to be the same size or colour. Since Mongo however can store pretty big objects, it is good to have index on such objects. So let's check how to create indexes:

Index

Indexes help with searching through mongo, as such there are a lot of indexes, depending what you need:

Index types

Single Field

In addition to the MongoDB-defined _id index, MongoDB supports the creation of user-defined ascending/descending indexes on a single field of a document.

Compound Index

MongoDB also supports user-defined indexes on multiple fields, i.e. compound indexes.

The order of fields listed in a compound index has significance. For instance, if a compound index consists of { userid: 1, score: -1}, the index sorts first by userid and then, within each userid value, sorts by score. For compound indexes and sort operations, the sort order (i.e. ascending or descending) of the index keys can determine whether the index can support a sort operation. See Sort Order for more information on the impact of index order on results in compound indexes.

Multikey Index

MongoDB uses multikey indexes to index the content stored in arrays. If you index a field that holds an array value, MongoDB creates separate index entries for every element of the array. These multikey indexes allow queries to select documents that contain arrays by matching on element or elements of the arrays. MongoDB automatically determines whether to create a multikey index if the indexed field contains an array value; you do not need to explicitly specify the multikey type.

Geospatial Index

To support efficient queries of geospatial coordinate data, MongoDB provides two special indexes: 2d indexes that uses planar geometry when returning results and 2dsphere indexes that use spherical geometry to return results.

Text Indexes

MongoDB provides a text index type that supports searching for string content in a collection. These text indexes do not store language-specific stop words (e.g. “the”, “a”, “or”) and stem the words in a collection to only store root words.

Hashed Indexes

To support hash based sharding, MongoDB provides a hashed index type, which indexes the hash of the value of a field. These indexes have a more random distribution of values along their range, but only support equality matches and cannot support range-based queries.

Index Properties

Unique Indexes

The unique property for an index causes MongoDB to reject duplicate values for the indexed field. Other than the unique constraint, unique indexes are functionally interchangeable with other MongoDB indexes.

Partial Indexes

New in version 3.2.

Partial indexes only index the documents in a collection that meet a specified filter expression. By indexing a subset of the documents in a collection, partial indexes have lower storage requirements and reduced performance costs for index creation and maintenance.

Sparse Indexes

The sparse property of an index ensures that the index only contain entries for documents that have the indexed field. The index skips documents that do not have the indexed field.

You can combine the sparse index option with the unique index option to prevent inserting documents that have duplicate values for the indexed field(s) and skip indexing documents that lack the indexed field(s).

TTL Indexes

TTL indexes are special indexes that MongoDB can use to automatically remove documents from a collection after a certain amount of time. This is ideal for certain types of information like machine generated event data, logs, and session information that only need to persist in a database for a finite amount of time.

Hidden Indexes

New in version 4.4.

Hidden indexes are not visible to the query planner and cannot be used to support a query.

By hiding an index from the planner, users can evaluate the potential impact of dropping an index without actually dropping the index. If the impact is negative, the user can unhide the index instead of having to recreate a dropped index. And because indexes are fully maintained while hidden, the indexes are immediately available for use once unhidden.

Except for the _id index, you can hide any indexes.

Practice

Let's see how to get what indexes a collection has and how to create or re-create and index:

Get Indexes

To get the indexes for a certain collection we can use the following function in any collection:

Get Indexes

Create an Index

To create index we can use:

Create Index

Reindex

Reindex will re-create all indexes on a collection (after mongoimport for example)

Re-Index

Execute a script

If you have a JSON file “js” which you would like to execute on the mongo, you can do as follows: