MongoDB is a NoSQL databases, used in real time, big data and social media applications.
MongoDB makes use of records which are made up of documents that contain a data structure composed of field and value pairs.
The documents are similar to JavaScript Object Notation, but use a variant called Binary JSON (BSON).
MongoDB is really powerful to organize and save your data in a complex structure, called documents. MongoDB data will be stored into database that are composed by collections. Every collection contains documents. A collection can contains different structure of documents. If you need you can strictly define the structure inside a document using schemas (we will see MongoDB schema when we will use the dependency "Mongoose". Schemas allow you writing MongoDB validation and casting too.
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.
A JSON formatter and validator
Register an account for free on MongoDB Atlas, and then create a new database, if you want to try for free, select a Shared one and check on the bottom if you are choosing a free hosting, then you can confirm the cluster creation.
Wait for the cluster to be ready and online, you can see it from the main dashboard. Remember to check and modify, if you need, the Database access. Finally, select your connection method and use it. You can find help by clicking on the Connect button.
Note: a free cluster will be paused if you will not use it for a while, if that will happen, you can just login into your dashboard and resume it.
We want to manage a warehouse database that contains various types of items. So we will have:
- users composed by id, name, surname (and maybe the creation date?)
- an item is normally composed by id, label, price, and quantity that refer to the amount of item that is available in the warehouse
- an item can have information about its color
- an item can be a complex item that is composed of parts
- an item part is described by a label and can has 3 types of warning (battery warning, liquid warning, and glass warning)
- an item part can be composed of other parts
- user orders composed by id, user_id, array of item_id, a sent and a closed flag (what about the quantity of the bought items?)
Compass is an application that allows us to manage our MongoDB database, we will see how to create collections, insert data and query them with this application (note: compass can do more than that). Download and install MongoDB Compass.
Connect to your database using MongoDB Compass, remember that if you are using Atlas, you can generate the connection string with the Connect button.
Your connection string shoudl be like: mongodb+srv://<username>:<password>@<hostname>/<database>
(without < and >).
Create your first database, when you create a new database you need to create one collection too. Following our use-case, we will create our database named "warehouse" with the first collection named "user". For the creation of a collection, we don't need the structure or the schema of our documents that will be contained in the collection, in fact, Compas is asking us only for the collection name.
Clicking on the database name we can list our collections, clicking the "+" we can create a new collection inside the database. Now, create the others collection that we need: "item", and "order".
After that, you should able to see in the Composed dashboard the 3 collections.

Clicking on the collection in our Compass dashboard will give us the functionalities to populate, and query our selected collection.
Here is a JSON that contains our first user (you can omit the _id that will automatically generate)
{
"name":"pluto",
"surname":"rossi"
}
Let's populate some items, and note that the items' document structure will be different, we will create items, colored items and complex items.
{
"label":"battery",
"price":2.5,
"quantity":10
}
{
"label":"computer",
"price":1696,
"quantity":3,
"parts":[
{
"label":"ram",
"price":64,
"quantity":4
},
{
"label":"cpu",
"price":320,
"quantity":1
},
{
"label":"graphic card",
"price":800,
"quantity":1
},
{
"label":"motherboard",
"price":120,
"quantity":1
},
{
"label":"case",
"price":50,
"quantity":1
},
{
"label":"power supply",
"price":150,
"quantity":1
}
]
}
{
"label":"pc portable",
"price":1200,
"quantity":2,
"color":"red"
}
{
"label":"pc portable",
"price":1200,
"quantity":3,
"color":"blue"
}
{
"label":"pc portable with cover",
"price":1250,
"quantity":1,
"color":"blue",
"parts":[
{
"label":"pc portable",
"price":1200,
"quantity":1,
"color":"blue"
},
{
"label":"cover",
"price":50,
"quantity":1,
"color":"blue"
}
]
}
You can see your documents with different visualization options.

Now let's try a simple query:{label:"pc portable"}
The query language is based on JavaScript.

Other examples of query:
{"parts.label":"ram"}{"parts": { $size: 2 } }{"parts":{$gt: {$size: 1}}}{"price":{$lte: 200}}
MongoDB doesn't have an easy way to create a join query and doesn't have triggers that we can use to propagate consistency into our collections.
For example, if we have two different collections, "user" and "car" and we know that there is a relation between them, every user can have one or more cars. How to create a query that will give us the full list that represents that relation? In a relational database usually (for example using SQL) we can use the JOIN operator, which will merge two or more tables and allow us to filter them, getting as a result the data of the two tables following the relation that we explicitate.
Using MongoDB, usually a backend service will cover these needs. This is good for scalability too because the database will have less responsibility and so will be able to scale better into a replicated distributed microservices architecture.
There are several ways to increase and manage performance using a microservice architecture, for example, referring to the image up here,
we have a load balancer for the DB section and another one for the backend section, every request received from the load balancer will be redirected to the less busy instance (is more complex than this, but keep it simple).
Every instance can be hosted inside a virtual machine or a docker image, which can increase or decrease the amount of resources like memory, and CPU.
Node.js is a cross-platform, open-source server environment. It is used for server-side programming, and primarily deployed for non-blocking, event-driven servers.
NPM (Node Package Manager) is used to manage the dependecies of your node.js service (Mongoose is a dependecy).
Download Node.js Coding with node.js, example and explanation
Create a new node.js project.
- create a new directory and inside run
npm init - install our dependency
npm i -s express,npm i -s cors, andnpm i -s mongoose - create the entry point file, as an example
index.js - let's coding
If you want to use the node project inside this repo, go into warehouse then
- run
npm install - change your DB connection string (first line
const DB) - run
node ./index.js
Now you can try those URLs on your browser:
http://localhost:4000/hellohttp://localhost:4000/hello?name=Andrea
A schema is a JSON object that defines the the structure and contents of your data, to define your application's data model and validate documents whenever they're created, changed, or deleted.
We decide to use the scheme for the collections 'user' and 'order' but not for 'item'.
In order to skip the document schema validation use { strict: false } as a parameter of the Schema.
- route to list users
- route to list orders
- route to get an item by ID
- route to add a user
- route to add order
- route to add item







