Diving Into The World Of NOSQL Databases With MongoDB.
What are NOSQL databases?
Before we start discussing about the MongoDB and how to use it, you should first build a basic understanding what are NOSQL databases and how are they different from the traditional SQL databases (Note: feel free to skip this part if you are already well versed in the basic concepts and explore the MongoDB section).
SQL VS NOSQL:
Well, SQL stands for “Structured Query Language” which is used on the “relational databases”. Relational databases have a defined structure and the data which is stored in these databases are held in the form of rows and columns. To create a better understanding see the following image:
Where as in the NOSQL databases you do not store data in the tabular or relational format rather you store data in document residing in a collection.
Meaning you have different named collection and each of them have one or more documents.
A typical NOSQL document looks like:
Advantages Of NOSQL Databases:
- Handle large volumes of data at high speed with a scale-out architecture.
- Store unstructured, semi-structured, or structured data.
- Enable easy updates to schemas and fields.
- Be developer-friendly.
- Take full advantage of the cloud to deliver zero downtime.
Some Popular NOSQL Databases:
- MongoDB
- Apache Cassandra
- Neo4j
- RavenDB
- Redis
- DynamoDB
MongoDB Setup
MongoDB is one of the most popular NOSQL database out there. It is a cross-platform database which stores the data in JSON like document. For this tutorial we will use the MongoDB compass which is an interactive tool for querying the mongo database.
So without further ado let’s begin.
Installation:
Go to the official MongoDB website: https://www.mongodb.com/
Then follow the following steps:
Then Download the latest version offered:
Follow the installation steps, once done you would get a folder at the specified location which would look something like:
If you have used the default settings then the MongoDB Compass would’ve also been installed on your computer. Which can be accessed from either the start menu or even from the search bar.
Open the app and you’ll be shown the following screen.
Connecting To Local Host:
Click the Connect button and you will be shown the following screen:
There are two ways to interact with the MongoDB, through the compass or directly from the shell.
As mentioned earlier we will explore the compass as it would be easy for you to understand, however I will create another blog for using the terminal to perform the CRUD operations and other queries on MongoDB.
MongoDB Using Compass
Creating Your First MongoDB:
Navigate to the database window from the left panel, press the create database, give your database a name, give your collection a name and that’s it.
Congratulations you just created your first NOSQL database:
Adding A Document Into Collection:
Click on the newly created database to go inside it.
It’s gonna open the following window:
This was the initial collection we created, now click on this collection to enter into it.
As you can see the collection is currently empty, there are multiple ways to add data into your collection, for instance if you already have a saved .json or .csv you can simply import it by clicking the import button.
However, we are creating a database from scratch so we will click on the add data button and enter a new document by selecting the Insert Document.
This will pop up a window, with a pre-assigned id to the document.
Note: You can delete that id and insert your data, Mongo will automatically assign it id again. Try it yourself !
Press insert and viola, you just created your first document inside a collection.
Note: In the NOSQL databases there is no restriction that all the documents should follow the same structure, meaning you can add another document with more or less fields, Try it yourself
Updating A Document:
Select the ‘pen’ icon to update the document:
I simply add another filed, press the update button and that’s it the document got updated.
Deleting A Document:
Simply press the ‘bin’ icon to delete a document.
Note: Did you notice? As mentioned earlier, the two documents have different fields, pretty cool right?
Conclusion
There is always more to learn, I would suggest that you play around with the MongoDB Compass to get more familiar with the NOSQL, and once you think you have done considerable exercises on it, take on a new venture to establish deeper knowledge. I will leave a link to official docs too if you wanna go ahead and masters the skills.
Links
https://www.agiratech.com/wp-content/uploads/2018/01/Difference-between-SQL-and-NOSQL-2.png