How to use mongodb in Ubuntu 20.04

MongoDB is a document-based non-relational database management system. It’s also called an object-based system. It offers faster query processing but with an increased load and system requirements.

In order to get started with Mongodb, you need to install Mongodb to your computer. To install the mongodb, you need to run this command.

sudo apt update
sudo apt upgrade
sudo apt install mongodb


#To check if the installation is successfull
sudo systemctl status mongodb
#To restart mongodb service
sudo systemctl restart mongodb
#To stop mongodb
sudo systemctl stop mongodb
#To uninstall mongodb
sudo systemctl stop mongodb
sudo apt purge mongodb

  • To open the MongoDB, run this command:
    mongo

Mongodb command

Show database available

  • To check the databases, type show databases; command

Create new database

  • To add new database, type use <name_of_db>

Check db in use

  • For checking what db in use, type db.

Add Collections

  • To add collection in the database in use, type <name_of_db>.createCollection(“sample”)

Insert data

  • To add data in the collection created, type <name_of_db>.<name_of_collection>.insert({name:”sample”})

Show data

  • To show the data in the collection, type <name_of_db>.<name_of_collection>.find()

Update data

  • To update data in collection, type <name_of_db>.<name_of_collection>.updateOne({<filter>},{<changes>}).
    Note: This updateOne command only update the first data it reads, notice the first name with new_sample_name.

Delete data

  • To delete a data in collection, type <name_of_db>.<name_of_collection>.updateOne({<filter_data_to_be_deleted>}
    Note: This deleteOne command only deletes the first data it reads.

For more detailed mongodb command:
https://cheatography.com/isaeus/cheat-sheets/mongodb/

Leave a Comment

Your email address will not be published. Required fields are marked *