MongoDB Database

MongoDB is document-oriented database management system designed for modern web applications and internet infrastructures. It scales easily and capable to store rich data structures. In this, data objects are stored inside a collection and database contains a set of different collections.

These are the some useful database related commands -

Login for Database Authentication

Before using the database, the user is required to authenticate using valid database credentials. Here is the command used for authentication.

db.auth("username", "password");

Show Databases

MongoDB show command is used to show all the current existing databases on MongoDB server.

show dbs;
mongodb show dbs

By default, mongodb has three existing databases.

  • admin - This is the root database. There are certain server side commands that permit to run on admin database only. If a user has permission to access admin database, then that user automatically gain permission to run all databases.
  • local - This is used to store any collections that are local to a server. We cannot replicate this database.
  • config - This is basically used in sharding to store information about the shards.

Create MongoDB Database

If you are familiar with relational database, it provides CREATE statement to create a new database and define structure. But there is no such CREATE command in MongoDB. The use command is used to create a new database or to choose the existing database for operation. If it does not exist, then it will create a database of the specified name. There is no need to define structure of database like RDBMS. This makes the MongoDB more dynamic and flexible.

Syntax of Create Database



use school;

The mongodb database contains group of collections. The database name can be -

  • Any UTF-8 string except these characters (/, \, ., ", *, <, >, :, |, ?, $, \0).
  • It is basically sticks with alphanumeric ASCII.
  • The database names are limited to a maximum of 64 bytes.

Current Database

To know the name of current using database, simply write db command.


Show Collections

The mongodb show collections command returns list of all collections created in the current database.

show collections;

Drop Database

The dropDatabase() command is used to drop the current database. Before using this command, first we need to select database using 'use' command.


{ "dropped" : "school", "ok" : 1 }

Backup Database

The mongodump utility is used for backing up database, by creating a binary export of database content.

>mongodump -h localhost --port 27017

Restore Database

The mongorestore utility is used for restoring data in database either from a binary database dump created by mongodump or from the standard input.

>mongorestore -h localhost --port 27017 dump

MongoDB Database Statistics

We can know about database statistics using stats() command. This is helpful in complicated debugging.

> db.stats();
        "db" : "school",
        "collections" : 3,
        "views" : 0,
        "objects" : 7,
        "avgObjSize" : 65.71428571428571,
        "dataSize" : 460,
        "storageSize" : 69632,
        "numExtents" : 0,
        "indexes" : 3,
        "indexSize" : 69632,
        "fsUsedSize" : 85098131456,
        "fsTotalSize" : 630906155008,
        "ok" : 1

Read more articles

General Knowledge

Learn Popular Language