Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Command to show the database currently being used in MongoDB?
The command to show the database currently used in MongoDB is the following −
Syntax
db;
This command returns the name of the currently active database in your MongoDB session.
Check Available Databases
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
Example 1: Check Current Database
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.
Example 2: Switch Database and Verify
Let us switch the database and verify again the correctness of command db. The query is as follows to switch database −
use test;
switched to db test
Now let us once again check the current database name. The query is as follows −
db;
The following is the output −
test
Alternative Method: Using getName()
You can also use the getName() function. The query is as follows −
db.getName();
The following is the output −
test
Conclusion
The db command is the simplest way to check your current working database in MongoDB. Both db and db.getName() return the same result, with db being more commonly used.
