MongoDB Commands

Dilshan Antony Thevathas
2 min readJun 10, 2022

--

Install MongoDB NoSQL Database:

MongoDB is very easy to install, and it is present in Ubuntu 20.04 software repository.

  1. sudo mongod
  2. sudo mongo
  3. show dbs
  4. use dbName
  5. db
  6. db.dropDatabase
  7. db.reateCollection(“collectionName”)
  8. db.collectionName.drop
  9. db.collectionName.insert()
  10. show collections
  11. db.collectionName.find()

Starting MongoDB Server

You can start MongoDB on your computer with the mongod command.

sudo mongod

sudo mongod command in termianal
sudo mongod command in termianal
waiting for mongo connection in terminal
Waiting for mongo connection in terminal

Keep the mongod window running when you want to work with your local MongoDB. MongoDB stops when you close the window.

Start MongoDB Client

One way to add items to a MongoDB database is through the Mongo Shell. To open up the Mongo Shell, you open another command line window and run mongo.

sudo mongo

sudo mongo command in terminal
Connected with Mongo
Connected with Mongo

Note: Make sure you keep the mongod window open! You won’t be able to interact with the Mongo Shell if you close the mongod window.

Show All Databases

Useshow dbscommand from mongo shell to list all the available databases on MongoDB server. This will show the database name with there size.

show dbs

show dbs command in terminal
show dbs command in terminal

I currently have three databases. They are: admin, config and local.

Go to a Particular Database

Create a database called databaseName. You can use the use <database> command to create and switch to a new database.

use databaseName

Create Database and Switched
Create Database and Switched

Create Collection

Create a collection called collectionName. You can use the db.createCollection("<collection name>")to create a new collection.

db.createCollection(“collectionName”)

Create a Collection
Create a Collection

--

--

Dilshan Antony Thevathas
Dilshan Antony Thevathas

Written by Dilshan Antony Thevathas

Hi! I'm Dilshan Antony Thevathas (AT Dilshan). I‘m a software engineer. I believe in helping people with my abilities and knowledge. Follow for more articles.

No responses yet