MongoDB | Create Database using MongoShell

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"
    },
]

MongoShell:
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 use.so 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 database.you 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.

Syntax:

 use Database_Name

Example: CREATING A NEW DATABASE

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.

Example: LIST OF DATABASES

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

Example: CHECKING CURRENTLY SELECTED DATABASE

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.

Example: SWITCHING TO ANOTHER 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.

References:
https://docs.mongodb.com/manual/core/databases-and-collections/#databases



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet



Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.