โ˜ How to use the NoSQL Database on Virtual Machine and CRUD Operations? (MongoDB)

Feyza Seyrek
Huawei Developers
Published in
4 min readDec 15, 2023
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 โ˜

Creating Linux Server

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
config codes

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.

creating user

Insert A Single Document ๐Ÿšฐ

You can insert a single document on MogoDB and see its information.

adding a single data

Insert Many Documents ๐Ÿšฐ

You can insert many documents on MongoDB and see their information. Also, you can see InsertedIds of Documents.

adding many documents
adding many 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.

updating data

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.

updating many documents

Delete Documents ๐Ÿšฎ

You can delete many documents or a single document. Also, you can see all information about the deleted data.

delete documents
delete many documents

Read All Documents In A Collection ๐Ÿ’ผ

reading all documents with find function

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.

References ๐Ÿ“—

--

--

Feyza Seyrek
Huawei Developers

๐Ÿ‘ฉโ€๐Ÿ’ป Cloud Solution Architect