Skip to content
Related Articles

Related Articles

MongoDB | Create Database using MongoShell

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 19 Sep, 2019

A MongoDB Database is the container for all the collections, where Collection is a bunch of MongoDB documents similar to tables in RDBMS and Document is made up of the fields similar to a tuple in RDBMS, but it has a dynamic schema here.

Example of a Document:

        "Name" : "Aman",
        Age : 24,
        Gender : "Male"    

Above document contains the information of a person in JSON format.
If we have a bunch of documents then it creates a collection. So we can say a User collection contains documents containing User Information.

Example of a Collection:

        "Name" : "Aman",
        Age : 24,
        Gender : "Male"    
        "Name" : "Suraj",
        Age : 32,
        Gender : "Male"    
        "Name" : "Joyita",
        "Age" : 21,
        "Gender" : "Female"
        "Name" : "Mahfuj",
        "Age" : 24,
        "Gender" : "Male"

The mongo shell is an interactive JavaScript interface to query and update data as well as perform administrative operations in MongoDB.

Databases: In MongoDB, databases basically holds the collections of documents. A database contains collection which contains the document. On a single MongoDB server, we can run multiple databases. Default created database of MongoDB is ‘db’ present within the data folder. When you install MongoDB some databases are automatically generated to we can say that it is not required to create a database before you start working with it.

Create a New Database : You can create a new Database in MongoDB by using “use Database_Name” command. The command creates a new database if it doesn’t exist, otherwise, it will return the existing can run this command in mongo shell to create a new database. Your newly created database is not present in the list of Database. To display database, you need to insert at least one document into it.


 use Database_Name


In MongoDB default database is test. If you did not create any Database and started inserting collection then all collections are stored in the Default Database.

Show list of Databases : You can check currently selected database, using the command “show dbs“. Your newly created database is not present in the list of Database. To display any database, you need to insert at least one or more document into it.


Check current Database : You can check list of databases, using the command “db


Switch to other Database : You can switch to other database using the command “use Database_Name“. If Database does not exists then it will create a new Database.


In the above Example, First we check current Database name using db command which was UserDB then we use “use test” command to switch to database test.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!