
The database must come first in MongoDB, followed by the collection in the database. As you may be aware, the database holds the collection, which we then use to store the documents.
We’ll learn database-related operations like CREATE, DROP, and VIEW, as well as collection-related activities, in this tutorial.
If you haven’t installed MongoDB yet then click here to know the complete steps of MongoDB installation.
Table of Contents
MongoDB Create Database with example
In MongoDB, the use command is used to create a database.
> use <database-name>
> use geekcer

View Current Database
The db command in MongoDB is used to check the current database.
> db

How to check database list in mongodb?
In MongoDB, we use show dbs command to view database list.
> show dbs

We created a database with geekcer, however it is not displayed above? This is because there is no document in the database.
> db.geekcer.insert({"IT":101})
> show dbs

You can also look at the database list in MongoDB Compass.

MongoDB Drop Database with example
> db.dropDatabase()
For your information first we check the list of databases available in mongodb before dropping.

Simple steps to drop database in MongoDB
Connect to database
> use geekcer

Drop the database
> db.dropDatabase()

List of databases after dropping database
> show dbs

You can also look into MongoDB Compass after dropping the database

MongoDB Create Collection with example
Collection stores the documents in MongoDB. A MongoDB collection is equivalent to a table in a relational database management system. MongoDB offers a command that creates a collection, similar to the CREATE TABLE command in RDBMS.
> db.createCollection(name, options)
> db.createCollection(name)
- name: The collection’s name is given here. This type is a string.
- options: This is an optional section where we can define memory size and indexing options. This is a type of document.

> show collections

Automatic Collection Creation in MongoDB
The collection is automatically created when you insert a document into MongoDB.
> db.employee.insert({"Name" : "CLARK", "Location" : "US"});

As you can see in the image, the employee collection is created automatically.
MongoDB Drop Collection with example
The MongoDB database relies heavily on collection. Of course, if you create a collection, you may need to delete it. We use the drop() method to drop a collection from the database.
> db.<collection_name>.drop()
Steps to drop collection
- Establish a database connection
- Before dropping, have a look at the collection list (Optional)
- Execute the drop collection command
- After dropping, look at the list of collections (Optional)
Here we are going to drop the employee collection.
> db.employee.drop()

Step 4 shows that the employee collection has been removed.