MongoDB | Create Database using MongoShell - GeeksforGeeks

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

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 DATABASEIn 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
nidhi_biet
MongoDB
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript
How to create footer to stay at the bottom of a Web page?
Differences between Functional Components and Class Components in React
Node.js fs.readFileSync() Method
File uploading in React.js
How to apply style to parent if it has child with CSS?
How to Open URL in New Tab using JavaScript ?