MongoDB Database
MongoDB is a document-oriented database management system designed for modern web applications and internet infrastructures. It scales easily and is capable of storing rich data structures. In this, data objects are stored inside a collection and a database contains a set of different collections.
These are 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;

By default, MongoDB has three existing databases.
- admin: This is the root database. There are certain server side commands that permit to run on the admin database only. If a user has permission to access the admin database, then that user automatically gains 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 a relational database, it provides CREATE statement to create a new database and define its 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 the structure of database like RDBMS. This makes the MongoDB more dynamic and flexible.
Syntax of Create Database
use DATABASE_NAME;
Example
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 a stick 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.
db;
Show Collections
The MongoDB show collections command returns a 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 the database using 'use' command.
Example
>db.dropDatabase()
{ "dropped" : "school", "ok" : 1 }
Backup Database
The mongodump utility is used for backing up the 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 the 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
}
>