Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » MongoDB » How to create Database and Collection in MongoDB?

  • Jagannath Rath Yatra History in Hindi | जगन्नाथ पुरी की कहानी
    Jagannath Rath Yatra History in Hindi | जगन्नाथ पुरी की कहानी Festival
  • जन्माष्टमी व्रत पूजा विस्तार से | दही हांडी | Krishna Janmashtami Puja
    जन्माष्टमी व्रत पूजा विस्तार से, दही हांडी: Krishna Janmashtami Puja Festival
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
    Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth Biography
  • Ganesh Chaturthi Puja in Hindi | गणेश चतुर्थी का व्रत, महत्व, कथा
    Ganesh Chaturthi Puja in Hindi | गणेश चतुर्थी का व्रत, महत्व, कथा Festival
  • Farmers Day
    National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती General Knowledge
  • Fundamental Duties of Indian Citizens
    Fundamental Duties of Indian Citizens | 11 मौलिक कर्तव्य हिंदी में General Knowledge
  • National Doctors Day in India and Other Countries, July 1, 2022
    National Doctors Day in India and Other Countries, July 1, 2022 General Knowledge
  • Why is Makar Sankranti celebrated
    Why is Makar Sankranti celebrated? Festival

How to create Database and Collection in MongoDB?

Posted on December 3, 2021December 3, 2021 By GeekCer Education No Comments on How to create Database and Collection in MongoDB?
How to create Database and Collection in MongoDB

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
    • View Current Database
    • How to check database list in mongodb?
  • MongoDB Drop Database with example
    • Connect to database
    • Drop the database
    • List of databases after dropping database
  • MongoDB Create Collection with example
  • Automatic Collection Creation in MongoDB
  • MongoDB Drop Collection with example
    • Steps to drop collection

MongoDB Create Database with example

In MongoDB, the use command is used to create a database.

> use <database-name>
> use geekcer
MongoDB Create Database with Syntax
MongoDB Create Database with Syntax

View Current Database

The db command in MongoDB is used to check the current database.

> db

Check the current db
Check the current database

How to check database list in mongodb?

In MongoDB, we use show dbs command to view database list.

> show dbs
How to check db list in mongodb?
How to check database list in mongodb?

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
View database list after inserting document
View database list after inserting document

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

Database list in MongoDB Compass
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.

List of databases available in mongodb before dropping
List of databases available in mongodb before dropping

Simple steps to drop database in MongoDB

Connect to database

> use geekcer
Connect to database
Connect to database

Drop the database

> db.dropDatabase()
Drop the database
Drop the database

List of databases after dropping database

> show dbs
List of databases after dropping database
List of databases after dropping database

You can also look into MongoDB Compass after dropping the database

MongoDB Compass after dropping the database
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.
Create Collections
Create Collection
> show collections
View Collections
View 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"});
Automatic Collection Creation in MongoDB
Automatic Collection Creation in MongoDB
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()
MongoDB Drop Collection with example
MongoDB Drop Collection with example
Step 4 shows that the employee collection has been removed.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

MongoDB Tags:How do you create a new database in MongoDB?, How does MongoDB show collection data?, How to Creating a database using “use” command, MongoDB create database script, What is create collection?, What is the syntax to create a collection?, What is the use of MongoDB database?

Post navigation

Previous Post: CRUD Operations in MongoDB | NoSQL | CRUD Transaction

More Related Articles

Learn MongoDB Tutorial, Installation, MongoDB DataTypes Learn MongoDB Tutorial, Installation, MongoDB DataTypes MongoDB
CRUD Operations in MongoDB | NoSQL | CRUD Transaction CRUD Operations in MongoDB | NoSQL | CRUD Transaction MongoDB

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved