Wednesday, October 14, 2020

Mongo DB - Basic Commands

This article aims to provide a list of basic commands used to navigate around in Mongo DB. All queries shown below have been tested in the MongoDB shell.

The following command will display a list of databases.

show dbs

The following command will create a new Mongo DB database (if it does not already exist). If the database already exists then the execution context will be switched to the named (in this example 'BookLibrary') database.

use BookLibrary

The following command will list the name of the database in which the current execution context is tagged with.

db

Use the following command to create a new collection. The following command shown below will create a collection named 'Books".

db.createCollection("Books")

The following command wil list the collections in the current database.

show collections

Use the following command to drop a collection. The below command drops a collection named 'Books'.

db.Books.drop()

In order to drop a database, use the following command. The below command drops a database named 'BookLibrary'.

db.DropDatabase()

These are some frequently used basic commands that will come handy when working with Mongo DB.

No comments:

Post a Comment