MongoDB & NoSQL

From Coders.Bay Wiki
Jump to navigation Jump to search

What is MongoDB[edit]

Introduction to MongoDB[edit]

MongoBD is one of the most popular NoSQL database currently, the format of storage is called 'BSON'. It means that MongoDB isn’t based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. It is a key value storage, but the database is document oriented (DODBMS).

A simple MongoDB document Structure

    title: "MongoDB is the best NoSQL DODBMS"
    by: "Emanuel Leutgeb"
    for: "Coders.Bay"
    type: "NoSQL"

And here a complex MongoDB document structure from an IGV company.

    "0": {
        "name": "STATION → STATION",
        "status": 0,
        "carrier": "epal",
        "order": {
            "recipient": {
                "0": {
                    "serialnumber": "@SERIALNUMBER"
            "action": {
                "0": {
                    "action": "pickup",
                    "target": {
                        "station": "@SOURCE;Source;station"
                "1": {
                    "action": "drop",
                    "target": {
                        "station": "@DESTINATION;Destination;station",
                        "sort": "station"
            "item": {
                "type": "epal"

SQL databases store data in tabulator format. So this data is stored in a predefined data model which is not so flexible.
Modern applications are more connected, interactive and need's to be flexible.
So all these Applications getting more and more data and need's to be flexible and accessing it are higher rates.

What are the main features of MongoDB[edit]
  • Document Oriented: MongoDB stores the main subject in the minimal number of documents and not by breaking it up into multiple relational structures like RDBMS (Relational Database Management System).
  • Indexing: Without indexing, a database would have to scan every document of a collection to select those that match the query which would be inefficient.
    So, for efficient searching Indexing is a must and MongoDB uses it to process huge volumes of data in very less time.
  • Scalability: MongoDB scales horizontally using sharding (partitioning data across various servers).
    Data is partitioned into data chunks using the shard key, and these data chunks are evenly distributed across shards that reside across many physical servers. Also, new machines can be added to a running database.
  • Replication and High Availability: MongoDB increases the data availability with multiple copies of data on different servers.
    By providing redundancy, it protects the database from hardware failures. If one server goes down, the data can be retrieved easily from other active servers which also had the data stored on them.
  • Aggregation: Aggregation operations process data records and return the computed results. It is similar to the GROUPBY clause in SQL.

Relational Database Management System (RDBMS) is not the correct choice when it comes to handling big data. If the database runs on a single server, then it will reach a scaling limit.
NoSQL databases are more scalable and provide superior performance. MongoDB is as already said a NoSQL database that scales by adding more and more servers and increases productivity with its flexible document model.

Why is MongoDB preferred over RDBMS like Oracle or MariaDB[edit]
  • Big Data: As already said, if you have huge amount of data to be stored in tables, think of MongoDB before RDBMS databases. MongoDB has built-in solution for partitioning and sharding your database.
  • Unstable Schema: Adding a new column in RDBMS is hard whereas MongoDB is schema-less. Adding a new field does not effect old documents and will be very easy.
  • Distributed data: Since multiple copies of data are stored across different servers, recovery of data is instant and safe even if there is a hardware failure.
What language is supported by MongoDB[edit]

MongoDB currently provides official driver support for all popular programming languages like C, C++, C#, Java, Node.js, Perl, PHP, Python, Ruby, Rust, Scala, Go, and Erlang.

What is NoSQL[edit]

Introduction to NoSQL[edit]

As already said NoSQL is a non relational database that provides a mechanism for storage and retrieval of data. NoSQL databases are used in real-time web applications and big data and their use are increasing over time.
NoSQL systems are also sometimes called Not only SQL to emphasize the fact that they may support SQL-like query languages. A NoSQL database includes simplicity of design, simpler horizontal scaling to clusters of machines and finer control over availability.
The data structures used by NoSQL databases are different from those used by default in relational databases which makes some operations faster in NoSQL.

When should NoSQL be used[edit]
  • When huge amount of data need to be stored and retrieved.
  • The relationship between the data you store is not that important.
  • The data changing over time and is not structured.
  • Support of Constraints and Joins is not required at database level.
  • The data is growing continuously and you need to scale the database regular to handle the data.

How to install it[edit]

Getting Started[edit]

There are two ways to work with MongoDB on your computer locally.

MongoDB with GUI[edit]

To install MongoDB locally you have to go to the MongoDB website and download the Community version for your operating system. Once downloaded you have to extract the folder and open the "install_compass" file.
Install compass.png
Now the GUI for MongoDB is installed.
Mongodb compass.png

If you don't want to install MongoDB via terminal skip the next step.

MongoDB with Terminal[edit]

To install MongoDB locally over Terminal you have to go to the MongoDB website choose your operating system and follow the construction to add it on your Terminal.
Now the Terminal version for MongoDB is installed.
Mongodb terminal.png

Working with MongoDB[edit]

Create your first Database[edit]

I highly recommend to use the Mongo Compass.
So to connect to the localhost just click "Connect" and the app will connect you to your localhost.
Mongodb compass connect.png

Now you can easy create a new database.

  1. Click "Create database".
    Mongodb compass create database.png
  2. Give the Database a name and the Collection too, like "TENNIS" and "PLAYERS".
    Mongodb compass name db.png
  3. Click now on the Database which was created by you for example "TENNIS".
  4. Click "Create collection".
    Mongodb compass create collection.png
  5. Give the Collection a name, like "TEAMS".
    Mongodb compass name cl.png

Insert your first Data[edit]

After you create the database and all of your collections, you can start inserting data to it.

  1. Click on a Collection where you want to add some data for EXAMPLE "PLAYERS".
  2. Click now on "ADD DATA" and then on "Insert Document".
    Mongodb compass insert document.png
  3. Now you can add your data inside.
    Mongodb compass insert yourdata.png
  4. But you also can add an existing file into it
    click on "ADD DATA" and then on "Import File".
    Mongodb compass insert file.png
  5. Now you can choose your file, the type and then import it to your Database.
    Mongodb compass insert yourfile.png

Create your first Query[edit]

After you import or insert all your data into the database you can start making some queries.
Just click on the "FILTER" type in your query and press "FIND".
Mongodb compass queries.png

For more information how to use it please visit the MongoDB manual.

Made with ♥︎ by Emanuel Leutgeb