MongoDB Databases
What Is Databases?
A MongoDB database is a container for collections. One MongoDB server can host many databases, each with its own collections and its own files on disk. In mongosh you switch databases with use, and the current one is always available as the db variable. A database is created lazily: it only really exists after you store data in it.
Why It Matters
| Reason | Benefit |
|---|---|
| Logical separation | Keep each application or environment (dev, test) in its own database on one server. |
| Access control | Roles can be granted per database, so a service user sees only its own data. |
| Simple cleanup | Dropping a database removes a whole test dataset in one command. |
| Lazy creation | You do not need a separate create step, which keeps scripts and demos short. |
How To Use It
- Connect with mongosh and run db to see the current database (usually test).
- Run show dbs to list databases that already contain data.
- Switch with use blog; mongosh replies that it switched to db blog.
- Insert one document, for example db.posts.insertOne({ title: "Hello" }).
- Run show dbs again and notice blog now appears.
- Check size and counts with db.stats().
- Remove it with db.dropDatabase() when you are done experimenting.
mongosh Examples
// Which database am I in?
db
// Switch (lazily creates "blog")
use blog
// Nothing is saved yet, so blog is not listed
show dbs
// First write makes the database real
db.posts.insertOne({ title: "Hello MongoDB", views: 0 })
show dbs
db.getName()
db.stats()
// Danger: removes the database you are currently using
db.dropDatabase() Edge Cases & Pitfalls
Empty databases do not show up
If you use a database but never write to it, show dbs will not list it. This is normal, not a bug.
dropDatabase affects the current database
Always run db to confirm where you are before calling db.dropDatabase(). There is no undo.
Naming rules
Names are case-sensitive, cannot be empty, and cannot contain characters such as /, \, ., a space, quotes, $, or the null character.
Do not store app data in admin, local, or config
Those databases are reserved for the server. Create your own database for application data.
Cheat Sheet
| Topic | Detail |
|---|---|
| use dbName | Switch the current database; creates it lazily on first write |
| db | Prints the name of the current database |
| show dbs | List databases that contain data (also: show databases) |
| db.getName() | Return the current database name as a string |
| db.stats() | Show size and object counts for the current database |
| db.dropDatabase() | Permanently delete the current database and all its collections |
| admin | Built-in database for administrative users and commands |
| local | Built-in database for server-local data such as the replication oplog; never replicated |
| config | Built-in database used by sharded clusters for metadata |
Key Takeaways
use myapp does not create anything by itself. The database appears in show dbs only after you insert a document or create a collection in it.
Frequently Asked Questions
How do I create a database in MongoDB?
Run use dbName in mongosh, then insert a document or create a collection. The database is created when the first data is stored.
Why does show dbs not list my new database?
Databases without any data are not shown. Insert a document into a collection in that database and it will appear.
How do I delete a database?
Switch to it with use dbName and run db.dropDatabase(). This permanently removes all of its collections and data.
What are admin, local, and config?
They are built-in databases. admin holds administrative data and users, local holds server-specific data such as the oplog, and config holds sharded cluster metadata.
Can I have many databases on one server?
Yes. A single mongod can host many databases. Many teams still prefer fewer databases with well-named collections, depending on access control and operational needs.
Is a database name case-sensitive?
Yes. Names such as Blog and blog refer to different databases.
Next: MongoDB Collections
Continue the MongoDB learning path.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
8 people found this page helpful
