โ How to use the NoSQL Database on Virtual Machine and CRUD Operations? (MongoDB)
Introduction ๐ฎ
Hi everyone ๐๐ , In this article, I will mention to you how to install a NoSQL database on a Linux Virtual Machine on Huawei Cloud and how to use MongoDB with a Cloud virtual machine and run CRUD operations. I wish you enjoyable reading โ๐คโ๐ซ
At the time of writing this article, the latest version of MongoDB available from the official MongoDB repositories is version 4.0. Before continuing with the next step visit the Install on Red Hat section of MongoDBโs and check if there is a new release available.
Create CentOS VM on Huawei Cloud โ
Installing MongoDB โฌ
To add the MongoDB repository to your system, open your text editor and create a new YUM repository configuration file named mongodb-org.repo
inside the /etc/yum.repos.d/
directory:
cd /etc/yum.repos.d/
mkdir mongodb-org.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
Using the yum program, install the mongodb-org meta-package now that the repository is enabled:
sudo yum install mongodb-org
Starting MongoDB โฉ
After installation is finished, launch the MongoDB daemon and set it to launch automatically by typing:
sudo systemctl start mongod
sudo systemctl enable mongod
Verifying MongoDB Installation โ
Using the Mongo tool, we will establish a connection to the MongoDB database server and output the server version in order to confirm the installation:
$mongo
db.version()
Configuring MongoDB ๐ง
You can configure your MongoDB instance by editing the /etc/mongod.conf
configuration file which is written in YAML.
security:
authorization: enabled
sudo systemctl restart mongod
Creating Administrative MongoDB User ๐
If you enabled the MongoDB authentication, create one administrative MongoDB user that you will use to access and manage your MongoDB instance.
mongo
use admin
db.createUser(
{
user: "mongoAdmin",
pwd: "changeMe",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Testing the administrative user ๐
quit()
$mongo -u mongoAdmin -p --authenticationDatabase admin
use admin
Create Users ๐น
You can create users on MongoDB. You can set users and their roles. You have to insert a username and password.
Insert A Single Document ๐ฐ
You can insert a single document on MogoDB and see its information.
Insert Many Documents ๐ฐ
You can insert many documents on MongoDB and see their information. Also, you can see InsertedIds of Documents.
Update A One Document ๐
You can update a single document. I am sharing my sample code blocks. You may get a response similar to the screenshot below.
Update Many Documents ๐
You can update many documents at one time. I`m sharing my example code blocks. You can customize your data for your requirements.
Delete Documents ๐ฎ
You can delete many documents or a single document. Also, you can see all information about the deleted data.
Read All Documents In A Collection ๐ผ
Conclusion ๐
To summarize, In this article, you have learned how to install and configure MongoDB 4.0 on your CentOS 7 server. You can also try to access the Mongo shell without any arguments and see if you can list the users using the same commands.