How to MongoDB- Create & Drop datatabse

In this guide, we are going to learn how to create & drop Database in MongoDB. A number of databases can be run on a single MongoDB server. There is no create database command in MongoDB. Default database of MongoDB is ‘db’.

Create MongoDB Database

To create a MongoDB database,

“Use” the database.

 

Syntax
    use DATABASE_NAME

The following example will create a database with name “lauyou”.

 

Example
    >use lauyou
    switched to db lauyou

To check your databases list, use the command show dbs.

 

Syntax
     MongoDB shell version: 1.8.1
     connecting to: test
     > show dbs
     admin   0.03125GB
     local   (empty)

Here, your created database “lauyou” is not present in the list, Define a collection named “users“, and save a dummy document inside.

Syntax
     >db.users.insert({"username":"anwar"})

Verify that you got what you wanted.

 

Drop MongoDB Database

The dropDatabase command is used to drop a database.

 

Syntax
     db.dropDatabase()

The following example deletes the database “lauyou”.

 

Example
     >use lauyou 
     switched to the db lauyou
     >db.dropDatabase()  
     { "dropped": "lauyou", "ok": 1}

 

Documents

The document is the unit of storing data in a MongoDB database. It uses JSON-style for storing data. Documents are analogous to the records of an RDBMS. Insert, update, and delete operations can be performed on a collection.

 

RDBMSMongoDB
TableCollection
ColumnKey
ValueValue
Records / RowsDocument / Object

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...