First, add the GPG key
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.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/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.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
mongosh
Modify the MongoDB configuration file (usually located at /etc/mongod.conf) to bind the MongoDB server to the IP address of the machine or to 0.0.0.0 to listen on all available network interfaces.[this is optional only if you want to allow external access to your DB]
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.