MongoDB Commands

Dilakshan Antony Thevathas
1 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

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

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

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 Collection

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

db.createCollection(“collectionName”)

--

--

Dilakshan Antony Thevathas

Hi! I'm Dilakshan. I'm a Software Engineer, and I believe in the great power of technology. My portfolio: https://atdilakshan.github.io/