MongoDB Create Databases and Collections
MongoDB Create Databases and Collections- MongoDB stores data in a Database → Collection → Document structure.
Database → Like a container for collections (similar to a database in SQL).
Collection → Like a table in SQL, it contains multiple documents.
Document → A JSON-like object that stores actual data.

Create a Database
1 |
use database_name |
If myDatabase
doesn’t exist, it will create a new database.
For checking the current database:
1 |
db |
To show all databases:
1 |
show dbs |

Create a Collection
1 2 3 |
db.createCollection("collection_name") // syntax db.createCollection("students") |
This will create an empty collection named students.
To view collections in the current DB:
1 |
show collections |
Delete a Collection
The drop()
method is used to delete an entire collection from a database in MongoDB.
Once a collection is dropped, all documents inside it are permanently deleted and cannot be recovered.
1 |
db.collection_name.drop() |
db
→ Refers to the current database.
collection_name
→ Name of the collection you want to delete.
drop()
→ Removes the collection.
Drop a collection of students
1 |
db.students.drop() |
Output will be:
true
Drop the Entire Database
1 |
db.dropDatabase() |
This will delete the whole database, including all collections.