Skip to content

MongoDB - Database Operations

By default MongoDB comes with 3 pre-installed databases: admin, config, and local. These databases are described below:

  • admin: Used to store system collections, user authentication and role information. Access limited to administrators of MongoDB.
  • config: Used to store internal information of the shard and cluster.
  • local: Used to store local information about the replication process and other related data.

Viewing Databases

To view all databases:

MongoDB
show dbs
Example Output
admin       40.00 KiB
analytics  184.00 KiB
config      72.00 KiB
local       72.00 KiB

Current Database

To view the currently selected (used) database:

MongoDB
db
Example Output
test

Selecting Database

To select (and use) a database, the command use can be used:

MongoDB
use myapp
Example Output
switched to db myapp

Info

Note that now when running the command show dbs to list the databases, our database is not yet visible. It will be visible after we create our first collection into it.

Removing (Dropping) Database

To remove (drop) a database, you need to switch into the database and the command db.dropDatabase() can be used.

Example below:

MongoDB
use test
db.dropDatabase()
Example Output
switched to db test
{ ok: 1, dropped: 'test' }