MongoDB – Create Database using Mongo Shell
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?
- Database: A MongoDB database is a container for all collections. A database can hold multiple collections, and each database is isolated from others.
- Collection: Collections are analogous to tables in relational databases. However, collections in MongoDB store documents in BSON format and don’t require a fixed schema.
- Document: A document is a set of key-value pairs, similar to a row in relational databases, but it has a flexible schema, allowing the addition of new fields without affecting other documents.
How to Create a MongoDB Database Using Mongo Shell
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:
use Database_Name
Create a New Database Using Mongo Shell Example
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 MongoDB
To 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 Example
The below example shows how to see list of databases using Mongo Shell.
Check Current Database
To 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 Example
The below image shows how to check current database using Mongo Shell.
Switch to Other Database
To 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 AnotherDB
Switch 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 Visible
Once 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.
Conclusion
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.
FAQs
How to create a database in shell script?
Use the command
mysql -e "CREATE DATABASE database_name;"
for MySQL orpsql -c "CREATE DATABASE database_name;"
for PostgreSQL.
How to create a table in MongoDB shell?
MongoDB doesn’t use tables like relational databases. Instead, you create collections with
db.createCollection('collection_name')
.
How to use MongoDB shell?
Start the MongoDB shell by typing
mongo
in your terminal, and interact with your database using commands likedb.collection.find()
ordb.collection.insert()