Command to show the database currently being used in MongoDB?


The command to show the database currently used in MongoDB is the following −

db;

Let us first check how many databases are present. The query is as follows −

> show dbs;

The following is the output displaying all the databases −

admin 0.000GB
config 0.000GB
local 0.000GB
sample 0.000GB
sampleDemo 0.000GB
studentSearch 0.000GB
test 0.003GB

Now, we have the list of all databases. Let us use the above syntax to check current database. The query is as follows −

> db;

The following is the output −

sample

Look at the above sample output, we are currently using ‘sample’ database. Let us switch the database and verify again the correctness of command db.

The query is as follows to switch database.

> use test;

The following is the output −

switched to db test

Look at the above sample output, we have changed the database from ‘sample’ to ‘test’.

Now let us once again check the current database name. The query is as follows −

> db;

The following is the output −

test

Use the function getName(). The query is as follows −

> db.getName();

The following is the output −

test

You can use the current command to check the current working database −

The query is as follows −

> db.current;

The following is the output −

test.current

Updated on: 30-Jul-2019

411 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements