Day purposes
✔️ Perform basic NoSQL queries, emphasizing MongoDB as a practical example.
✔️ Explore NoSQL database management tools.
✔️ Grasp the fundamentals of NoSQL databases, with a focus on MongoDB.
💡 This step is designed exclusively for participants who have successfully completed the SQL workshop earlier today. Please note that it is not necessary to proceed with the NoSQL workshop if you plan to attend the ORM session in the afternoon. The NoSQL workshop is entirely optional, and the ORM workshop that follows is considered more essential for the overall learning progression.
During the day 01, you learned a programming language to
develop software. But a software isn't only composed of a hundred thousand
lines of code, it's common to use external tools to take in charge a specific
task. 😉
For example, you can use Prometheus/Grafana to monitor your app, Kafka as a queue or a database to store huge amount of data...
It's an organized space where you can store pieces of information.
Each time you need a permanent storage, for example, to store users, you will
need a database.
It has many usage and ways, the most popular is SQL database or
also called relational database. However, today, we will delve into the world of NoSQL databases, and more specifically, document databases like MongoDB.
Let's dive into the exciting world of NoSQL databases! 🚀
Today we will learn document database but other exists:
You can find more information about databases in this post 😄
There are many tools to manage a database. We give you the choice between DataGrip and Compass IDE.
We recommend DataGrip for its powerful UX and easy adoption 😉
In the folder resources, you will find a file named database.json to get the data that we will generate in ou new database with artists and musics.
In document databases, data is stored into a document where each information is stored in an object as a field-and-value.
You won't be using traditional relations as in a relational database, where you create relationships between tables 😄. Instead, in a document database, you have the flexibility to either embed related data directly within a document or use references to establish connections between documents.
💡 You can find more information about MongoDB concepts here.
These concepts are important, if you are lost don't hesitate to ask the staff for help they'll be happy to help you understand 😜
If you don't want to use DataGrip, move to the Compass IDE setup.
First, download DataGrip using the Jetbrains Toolbox.
You can use docker to run a MongoDB database and npm or yarn to run the script to fill the documents
docker run -d \
--name poc-mongo-db \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=pass \
-v db:/var/lib/mongodb/data \
mongo:latest
Go to the resources folder and execute the setup.sh bash script.
cd resources
chmod +x setup.sh
./setup.sh
docker exec -it poc-mongo-db mongosh --username admin --password pass
If you can't manage to connect to your database, try changing the MONGO_INITDB_ROOT_USERNAME parameter to something else and reloading your container using
docker rm
.
Don't worry about this command for now, you will learn docker during day04 👀
Start DataGrip and create a new Data Source
of type MongoDB
.
Before entering the information chose the authentication method User & Password
Here's the information to fill in the form:
- Database name:
admin
- Username:
admin
- Password:
pass
- Host:
localhost
- Port:
27017
💡 You will certainly have to download the MongoDB driver on your first connection.
Below you have an example of configuration:
After applying the configuration, you should see a new data source in the left panel of DataGrip.
Verify that you have something similar to the example below:
To execute your commands go into the terminal logo and select the Open Default Console
so you can execute your firsts commands! 😎 As you can see once you write a command you can click on the gree ⏯️ button.
You can look a these steps if you encounter an issue during the configuration.
This is a official IDE by MongoDB for MongoDB
- Go to MongoDB Compass.
- Click on
Platform
and select your distribution (Ubuntu, Fedora, ...). - Click on
Download
. - Go to your
Downloads/
folder. - Execute this command:
sudo dpkg -i mongodb-compass_1.41.0_amd64.deb
- Open compass from you applications
You should get the following result
Go to Advanced Connection Options
and you should fill the username
and the password
with admin and pass
✅ To execute mongoDB commands in compass you can click the the
mongosh icon
on the bottom of the page and you will get a terminal.
If you correctly followed the requirements, you should have a database ready to use 😍
If your are here it means you finished the SQL part before the ORM part starts, 🥳 congratulations.
First of all create a noSQL folder in your day02
directory
mkdir -p day02/noSQL
Create a file queries.md
in which you will write every query you make to keep a trace:
touch queries.md
Your database is ready to run your first requests 🥳
The goal of this step is to understand how to read data in a database using mongosh.
Let's try to get some information from the collection artists
.
Write 4 queries to :
- List all the avaiable DB's and switch to your poc-mongo-db
- Retrieve all the information contained in the
artists
collection. - Retrieve only
name
andgenre
from the collectionartists
. - Retrieve the list of all
artists
ofgenre
hip-hop/rap
.
See how to read data with mongosh.
In a relational database, you store each individual entity in its own table, and link them together through foreign keys. While MongoDB certainly supports references from one document to another, and even multi-document joins, it’s a mistake to use a document database the same way you use a relational one.
Let's write 3 new queries to link information from collections:
- Retrieve
name
fromartists
andmusics
.
You must specify the name of your result field withartists_names
andmusics_names
. - Retrieve all
artist
who singed in the musicWe Are The World
.
Those artists must be sorted indescending
order according to their number of fans. - Retrieve all the
musics
fromBooba
.
They must be sorted inalphabetical
order.
- See how to sort data and aggregation stages .
> - An article about which method you should use to manage "relations" in a noSQL database, each one has pros and cons, [embed or references](https://www.reddit.com/r/mongodb/comments/nag9yd/references_vs_embedding/)
Yesterday, you programmed the CRUD of a resource, let's learn how to do it using noSQL 💪
Write queries to:
- Add a new
artist
with hisid
set to100
. - Delete all musics that have the
Gold
certification
. - Add the music
Take What You Want
to theartists
you previously created.
- See how create or delete with in mongosh.
- Did you know that you can create an use variables like in JavaScript?
You've learned the basics, let's see more advanced features with mongosh functions.
You will use operations to count elements in your collections.
💡 Databases are faster than any programming language (except C) so if you can pre-process your data in your query, do it.
Write 4 new queries to:
- Count the number of
artists
- Count the number of
artists
in eachgenre
. - Count the number of
musics
sorted by their certification and displayed in ascending order.
Congratulation, you now have solid knowledge in SQL and noSQL 🎉
Here are some links for the most courageous among you:
- Use Indexing for Performance Optimization
- MongoDB Aggregation Pipeline Queries vs SQL Queries
- Implement Caching Strategies
🚀 Don't hesitate to follow us on our different networks, and put a star 🌟 on
PoC's
repositories.