• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Create and Drop database in MongoDB

Written by Rahul, Updated on May 1, 2014

In earlier articles we have provided the steps to install MongoDB on CentOS and RHEL or Ubuntu and Debian Systems. Now find this article to how to create and drop database in MongoDB.

mongodb-medium-logo

1. Create Database in MongoDB

It’s strange to listen but true that MongoDB doesn’t provide any command to create databases. Then the question is how would we create database ?. The answer is – We don’t create database in MongoDB, we just need to use database with your preferred name and need to save a single record in database to create it.

List Databases – First check the current databases in our system.

# mongo
> show dbs;
admin  (empty)
local  0.078GB
test   0.078GB

Use New Database –
Now if we want to create database with name exampledb. Just run following command and save a single record in database. After saving your first example, you will see that new database has been created.

> use exampledb;
> s = { Name : "TecAdmin.net" }
> db.testData.insert( s );

List Databases –
Now if you list the databases, you will see the new database will be their with name exampledb.

> show dbs;
admin  (empty)
local  0.078GB
exampledb   0.078GB
test   0.078GB

2. Drop Database in MongoDB

MongoDB provides dropDatabase() command to drop currently used database with their associated data files. Before deleting make sure what database are you selected using db command.

> db
exampledb

Now if you execute dropDatabase() command. It will remove exampledb database.

> db.dropDatabase();
{ "dropped" : "exampledb", "ok" : 1 }

To delete MongoDB database from Linux command line or Shell Script – Use following command from

# mongo exampledb --eval "db.dropDatabase()"

References:
1. http://docs.mongodb.org/manual/tutorial/getting-started/
2. http://docs.mongodb.org/manual/reference/command/dropDatabase/

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

4 Comments

  1. Avatar Ashwini Reply
    October 20, 2020 at 5:28 am

    Which software we should use to execute mongodb for Android

  2. Avatar ASHUTOSH MORE Reply
    August 16, 2019 at 4:27 pm

    All MONGODB QUERIES SHOULD BE PROPERLY GIVEN ALL THE TIME .HELP STUDENTS.

  3. Avatar Ferrero Reply
    April 17, 2018 at 5:16 pm

    Can you tell me where do I have to write those comands please?

    • Rahul Rahul K. Reply
      April 18, 2018 at 4:10 am

      Use mongo shell https://tecadmin.net/tutorial/mongodb/mongo-shell/

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Ubuntu 20.04 5
  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy