For those of you who have not heard of MongoDB yet, this can be categorized as a document-oriented database system.
Classified as a NoSQL database, MongoDB uses JSON documents with dynamic schemes as opposed to the relational database structure, which makes data integration faster and more comfortable in certain types of applications.
MongoDB is an open-source NoSQL database written in C ++.
It can contain multiple databases, collections, and indexes. In some cases, these objects can be created by default. Once created, they are in the db.systems.collection, db.system.indexes catalog.
Collections contain documents (BSON). These documents also contain several fields. There are no predefined fields in MongoDB, unlike relational databases, where there are columns that are defined when tables are created. There is no schema for the fields in a document, and these types may vary. So there is no “alter table” to add columns.
In practice, it is common for a collection to have a homogeneous structure, although it is not a requirement, collections may have different structures. This flexibility implies ease in migrating and modifying the overall image of the data.
Installing MongoDB is extremely simple. This database can be hosted on various operating systems, including Windows, Linux, Ubuntu, Debian, and OS X.
How to Install MongoDB on Ubuntu
MongoDB is included in the Ubuntu package archive, but MongoDB’s official repository offers the latest versions and is the most recommended way to install the software. In this step, we will add this repository to our server.
Ubuntu ensures the authenticity of the software packages by checking if they are signed with GPG keys, so for the first time we will need to import the key from MongoDB’s official repository.
sudo apt-key adv - keyserver hkp: //keyserver.ubuntu.com: 80 -recv EA312927
After successfully importing the key, you will see the following message:
output:
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
Next, we’ll need to add the MongoDB repository details to be able to know where to download the packages. Launch the following command to create a list of MongoDB files.
sudo echo "deb http://repo.mongodb.org/apt/ubuntu trusts / MongoDB-org / 3.2 multiverse" | sudo tee /etc/apt/sources.list.d/MongoDB-org-3.2.list
After adding the archive details, we will need to update the package list.
sudo apt-get update
Installing and Verifying MongoDB
Now you can install MongoDB packages yourself.
sudo apt-get install -y --allow-unauthenticated MongoDB-org
Note: The MongoDB packages we use do not meet the signature power standards that Ubuntu expects and will need to install them only with the –allow-unauthenticated accessory. This command will fix a few packages containing the latest stable versions of MongoDB along with management tools for the MongoDB server.
In order to properly launch MongoDB as a service on Ubuntu, we will need to create an additional file describing the service. A single file systemd communicates how to use the resource. The most common type of unit is service that determines how we can start or stop the service when it should be automatically started at root, and whether it is dependent on other software to run.
We will create a drive file to handle the MongoDB service.
Create a configuration file called mongodb.service in the / etc / systemd / system directory using your favorite nano or editor.
sudo nano /etc/systemd/system/mongodb.service
Paste the following content, then save and close the file.
/etc/systemd/system/mongodb.service
[United]
Description = High-performance, schema-free document-oriented database
After = network.target
[Service]
MongoDB User =
ExecStart = / usr / bin / mongod –quiet –config /etc/mongod.conf
[Install]
WantedBy = multi-user.target
This file has a simple structure:
The Unit section contains the general presentation as well as the dependencies that need to be fulfilled before the start of the service. in our case, MongoDB already depends on the network, as here network.target.
The Service section is about how the service will begin. The User statement specifies that the server will run under the MongoDB user and the ExecStart statement defines the start command for the MongoDB server.
The last section, Install, says systemd, when the service will start multi-user.target automatically is a standard startup sequence of the system, which means the server will start automatically during boot.
Next start the newly created service with systemctl.
sudo systemctl start MongoDB
Since there is no output for this command, you can use systemctl to check if the service has started correctly.
sudo systemctl status MongoDB
output:
MongoDB.service – High-performance, schema-free document-oriented database
Loaded: loaded (/etc/systemd/system/mongodb.service; enabled; vendor preset: enabled)
Active: active (running) since Mon year month of the day EDT hour; 2min 55s ago
Main PID: 4077 (mongod)
Tasks: 16 (limit: 512)
Memory: 32.1M
CPU: 1.444s
CGroup: /system.slice/MongoDB.service
└─4077 / usr / bin / mongod –quiet –config /etc/mongod.conf
The last step is to enable MongoDB’s automatic startup when the system starts.
sudo systemctl enable MongoDB
The MongoDB server is now configured and running to manage the MongoDB service using the systemctl command.
sudo systemctl MongoDB stop
sudo systemctl MongoDB start
Also, the Mongo distribution includes the bin/mongo file that is an interactive shell written in JavaScript and used in the same way that SQL Plus for Oracle database commands is used for command line commands. Shell is useful for test checks and administrative functions.