How to Create and Drop database in MongoDB NoSQL Database Servers

undefined

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 NoSQL database server in a very easy steps

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 : "mimastech.com" }
> 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()"

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

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