Last Updated : 11 Jul, 2025
MongoDB is a popular NoSQL database that uses collections and documents, which are highly flexible and scalable. Unlike relational databases (RDBMS), MongoDB does not use tables and rows but stores data in a more dynamic, JSON-like format.
In this article, we'll explore how to create a MongoDB database using the Mongo Shell and cover related database operations.
What is MongoDB Database, Collection, and Document?To create a new MongoDB database using Mongo Shell use the "use Database_Name" command. This command creates a new database if it doesn't exist, otherwise, it will return the existing database.
The newly created database will not be present in the list of databases. To display the database in the database list, insert at least one document into it.
Syntax:
Create a New Database Using Mongo Shell Exampleuse Database_Name
The below image shows new database creation using Mongo Shell:
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.
How to View the List of Databases in MongoDBTo view a list of all existing MongoDB databases, use the following command:
show dbs
This will return a list of all databases currently stored in MongoDB. Note that if your new database doesn't contain any collections, it won’t appear in this list until you add a document to it.
Show List of Databases ExampleThe below example shows how to see list of databases using Mongo Shell.
Check Current DatabaseTo check the current database you're working with, simply use the following command:
db
This will return the name of the database that’s currently active. If you’ve just created a new database, it will return the name of the newly selected database.
Check Current Database ExampleThe below image shows how to check current database using Mongo Shell.
Switch to Other DatabaseTo switch to a different database, use the use
command again followed by the name of the desired database. If the database does not exist, MongoDB will create it:
use AnotherDBSwitch to other Database Example
The below image shows how to switch to another database using Mongo Shell.
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.
Insert the First Document to Make the Database VisibleOnce you’ve created your database, you can insert a document into a collection to make the database visible in the database list. Here’s an example of inserting a document into a new collection:
db.users.insert({ name: "John Doe", age: 30 })
Once we insert a document, the TestDB
database will appear in the list of databases when we run the show dbs
command.
Creating a MongoDB database using the Mongo Shell is simple and intuitive. We can easily create, switch between, and manage MongoDB databases through the shell. MongoDB’s flexible schema model allows us to quickly adapt our databases to the changing needs of your application. Remember, the database only becomes visible in the list after inserting the first document into a collection.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4