NoSQL, MongoDB, Mongo Express, and Docker
NoSQL
NoSQL databases are built for specific data models and have flexible schemas. The main objective is to satisfy the needs that SQL cannot satisfy. One of these needs, for example, is to allow new application models while the old model continues to function.
Main differences
SQL
- Follows ANSI standard;
- Own language;
- Table storage;
NoSQL
- Does not follow a pattern;
- Has its own query language;
- Has its own storage structure;
Why use?
- Flexibility;
- Scalability;
- High Performance;
- Highly Functional;
Types of NoSQL Databases
Graphic: In a graph-oriented database, there are no tables, documents, or any other structure that is comparable to a table. In this type of database, everything is nodes (vertices) and relationships (edges). The most popular one is Neo4j.
Key/Value: All records are part of the same collection of elements. Making a comparison with relational databases, it is as if the entire database were a single table that has only two columns: a key, and another with value. An example of this type is Redis.
Columnar: The main structure of columnar databases is basically a large table. Each record can have as many and as many columns as needed (schemaless). Columnar databases are the most similar to relational databases in that they have a “table” in their structure, even though they are actually quite different since the data is column-oriented internally. An example of this type is Cassandra.
Document: The document-style database uses the equivalent of an object as its smallest logical unit. And a collection can contain multiple documents. Because there is no fixed schema, each document can have a different number and type of field, which adds to the flexibility. An example of this type is MongoDB.
MongoDB main characteristics
- Open source;
- Runs in Linux, Mac, and Windows;
- Versions Community, Enterprise, and Atlas;
- Uses document format, which is stored inside collections. These documents have a structure very similar to JSON, with key and value.
- The schema is not fixed. It means that we can have, within the same collection, several documents that have different fields. With this structure, MongoDB manages to facilitate the restructuring of a Database.
- Provides replicas. With these replicas, it guarantees greater data availability, since, in addition to the main server, we can have two more copies of this server on other machines.
Now, let’s run MongoDB using Docker
Create a docker-compose.yml file:
version: '3.1'
services:
mongo:
image: mongo
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8084:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/
Run with this command:
docker-compose up
Now, access http://localhost:8084 using a browser.
Mongo Express
Mongo Express is a web-based interface for graphically managing MongoDB databases. It is built using Node.js, Express, and Bootstrap packages.
Creating a database
Put a name and click on“Create Database”.
Creating a collection
Click on “db02” or the name you gave to your db.
And fill in a name and click on “Create Collection”
Now, you are looking at the collection you almost created.
Creating a document
Now, let’s create a document. It’s simple, click on “New Document” and add a value like this:
The ObjectID() function generates an id, you can insert a value for id, for example:
The objects created are here:
Using these buttons, you can remove all or one of them.
Clicking on one line you can consult and edit the object:
That’s it for today.