MongoDB Database
MongoDB Database is a logical container for MongoDB Collections. Each MongoDB collection stores one or more MongoDB Documents, and each document stores data in a flexible field-value structure.
In simple terms, a MongoDB database groups related collections together. For example, an application database may contain collections such as users, orders, products, and payments. Unlike a fixed table-first design, MongoDB lets collections store documents whose fields can vary when the application needs that flexibility.
MongoDB Database, Collection, and Document Structure
The relationship between database, collection, and document can be understood as a hierarchy.
MongoDB deployment
└── Database
└── Collection
└── Document
A database is the top-level namespace used by an application or a module. A collection is similar to a group of related records. A document is the actual data record stored in BSON format, commonly represented in examples as JSON-like data.
Common MongoDB Database Operations
Following are the Operations that could be done on a Database in MongoDB.
In day-to-day use, you may also list databases, check the current database, switch between databases, create collections, insert documents, and remove databases that are no longer needed.
USE DATABASE Command in MongoDB
To start using or switch to a Database, use the following command
use <database_name>
The use command changes the current database context in MongoDB Shell. If the database name does not already exist, MongoDB switches to that database name, but the database is not listed by show dbs until data is actually stored in it.
Following is an example to switch to a database. Run the command in MongoDB Shell.
> use tutorialkart
switched to db tutorialkart
Now you have been switched to tutorialkart Database.
Check the Current MongoDB Database
After switching databases, you can verify the current database by running the db command in the shell.
db
tutorialkart
This output confirms that further collection and document operations will be executed in the tutorialkart database unless you switch to another database.
List Available MongoDB Databases
To list databases that already contain data, use show dbs.
show dbs
admin 40.00 KiB
config 72.00 KiB
local 72.00 KiB
tutorialkart 8.00 KiB
If you switch to a new database name but do not create a collection or insert a document, that empty database may not appear in the list. This behavior is normal in MongoDB.
Create Data Inside a MongoDB Database
A MongoDB database becomes visible after it contains data. One common way is to create a collection and insert a document.
use tutorialkart
db.students.insertOne({
name: "Ravi",
course: "MongoDB",
status: "active"
})
{
acknowledged: true,
insertedId: ObjectId("...")
}
In this example, MongoDB uses the tutorialkart database and creates the students collection when the first document is inserted, if the collection does not already exist.
When to Use Separate MongoDB Databases
Use separate MongoDB databases when the data belongs to separate applications, tenants, environments, or administrative boundaries. For example, a development database and a production database should usually be separate. Similarly, two unrelated applications should not share the same database unless there is a clear design reason.
Use separate collections inside the same database when the data belongs to the same application but represents different entities. For example, an online store may keep customers, orders, and products as different collections in one application database.
MongoDB Database Naming Guidelines
Choose database names that are short, clear, and related to the application purpose. Avoid names that are confusing, temporary, or too generic, such as test1 or newdb, for long-term application data.
- Use lowercase names where possible, such as
tutorialkartorschool_app. - Avoid spaces in database names.
- Keep development, testing, and production databases clearly separated.
- Do not store unrelated application data in the same database just for convenience.
- Before deleting a database, confirm the current database with the
dbcommand.
MongoDB Database Commands Summary
| Task | MongoDB Shell Command | Purpose |
|---|---|---|
| Switch to a database | use database_name | Changes the active database context. |
| Check current database | db | Shows the database currently selected. |
| List databases | show dbs | Displays databases that contain data. |
| Create collection explicitly | db.createCollection("collection_name") | Creates a collection in the current database. |
| Insert a document | db.collection_name.insertOne({...}) | Adds a document to a collection. |
| Delete current database | db.dropDatabase() | Drops the selected database. |
Mistakes to Avoid While Working with MongoDB Databases
- Do not assume that
use database_nameimmediately creates a visible database. Store data first. - Do not run
db.dropDatabase()without checking the active database. - Do not mix development and production data in the same database.
- Do not create too many databases when separate collections are enough.
- Do not rely only on database names for security; configure users, roles, and access controls properly.
FAQ on MongoDB Database
What is a MongoDB database used for?
A MongoDB database is used to group related collections for an application or a module. It stores data as documents inside collections, making it useful for applications that work with flexible or evolving data structures.
Does the MongoDB use command create a database immediately?
The use command switches the current database context. If the database does not exist, MongoDB accepts the name, but the database becomes visible in database lists only after data is inserted or a collection is created.
What is the difference between a MongoDB database and a collection?
A MongoDB database is a container for collections. A collection is a container for documents. For example, school can be a database, students can be a collection, and each student record can be a document.
How do I check which MongoDB database I am using?
Run the db command in the MongoDB shell. It prints the name of the current database.
Why is my new MongoDB database not shown in show dbs?
A newly selected database may not appear in show dbs until it contains data. Insert a document or create a collection with data, then run show dbs again.
Editorial QA Checklist for MongoDB Database Tutorial
- Verify that the tutorial clearly explains the database, collection, and document hierarchy.
- Confirm that the
use,db, andshow dbsexamples are shown with correct MongoDB shell behavior. - Check that the tutorial explains why an empty MongoDB database may not appear in
show dbs. - Ensure delete-related instructions remind readers to confirm the active database before using
db.dropDatabase(). - Keep examples simple enough for beginners using MongoDB Shell or mongosh.
Summary of MongoDB Database Usage
In this MongoDB Tutorial, we learned what a MongoDB database is, how it relates to collections and documents, how to switch databases using the use command, how to check the current database, and why a new database appears only after it contains data.
TutorialKart.com