How to Install MongoDB in Ubuntu Inside AWS

First, add the GPG key

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

Next, we need to create a list file for MongoDB

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

Then, update the APT package list.

sudo apt-get update

Now it’s time to install MongoDB

sudo apt-get install -y mongodb-org

If you have typed everything correctly, then it should install MongoDB on your Ubuntu Machine.

Next, we need to start the MongoDB by running this command

sudo systemctl start mongod

Verify that it started, by running this command.

sudo systemctl status mongod

Next, we need to enable MongoDB when the system reboots, to do that, please run this command.

sudo systemctl enable mongod

Now, to run the mongo Shell and insert some data, please run this command


mongo

Once you have finished the login to Mongo Shell, then you can insert data into collections. First, switch to any database by running the following commands.

use erpdatabase

Then run this command to insert a row.

db.users.insertOne(
   { firstName : "Atiqur",
     lastName : "Rahman",
     username : "randomuser0298",
     age : 35 , 
     gender : "Male",     
   }
)

So once you have inserted the data, then you can search that data using the find method.

db.users.find(
   {
     username: "randomuser0298"
   }
).limit( 10 ) 

A complete step-by-step guide created which can be watched any time and practice on your own AWS Account.

Spread the love