EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB List Databases
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB JSON
    • MongoDB Careers
    • MongoDB Migration
    • MongoDB Export to Excel
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
    • MongoDB schema
    • MongoDB find by id
    • Mongodb max document size
    • Mongodb not equal
    • MongoDB keyfile
    • MongoDB Hosting
    • MongoDB Kafka Connector
    • MongoDB exists
    • MongoDB express
    • MongoDB Performance
    • MongoDB Connection String
    • MongoDB BI Connector
    • Mongodb aggregation pipeline
    • Mongodb backup and restore
    • MongoDB Drop Collection
    • MongoDB Text Search
    • MongoDB URI
    • MongoDB Full Text Search
    • MongoDB List Databases
    • MongoDB Use Cases
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

MongoDB List Databases

MongoDB List Databases

Introduction to MongoDB List Databases

MongoDB provides the different types of commands to the user, in which that list database is one of the commands that MongoDB provides. Basically, the MongoDB list database command is used to list all existing databases from the current MongoDB server; it can be either system generated or user created. The list database command we run against the admin database. As per the MongoDB version, the list database command may have a different syntax that means it depends on the version of MongoDB. We can perform the list database command on the MongoDB shell as well as the MongoDB compass.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

show dbs;

Explanation:

  • In the above syntax, we use the show command to list all existing databases on MongoDB as per requirement. Here dbs means database.

How to List Databases in MongoDB?

Given below shows how to list database in MongoDB:

Basically, there are two different ways to list the database one is we can use the show dbs command, and another one is we can use the listDatabase command.

Basically, the listDatabase command is used for the MongoDB application as per our requirement. Normally the listDatabase command is used to list all existing databases with all basic details, but the only difference is that in the listDatabase command, we need to execute the against the admin database by using the following command as follows.

Code:

db.adminCommand( { listDatabases: 1 } )

Explanation:

  • In the above statement, we used db.adminCommand with listDatabase command as shown; here, we use 1 for output. When we use 1 with the listDatabase command, then it does not affect the output.

Different options of the listDatabase command are as follows:

  • filter: This is an optional part of listDatabase; the filter option is used to determine which database we need to display. The filter has different options such as name, sizeOnDisk, empty, and shared. As per our requirement, we can use any one of the options.
  • nameOnly: This is another option in the listDatabase command, and it is optional. A field is used to show whether the order should return simply the data set names or return both data set names and size data. Returning size data requires locking every data set each in turn, while returning just names doesn’t need locking any data set. The default esteem is bogus, so listDatabases returns the name and size data of every information base.
  • authorizedDatabases: This is an optional part of the command. This option that figures out which data sets are returned depends on the client’s advantages when access control is empowered. In this option, there are two options as follows. If the user has permission, then it can be able to list all the databases, and if the user does have any permission, then it is able to list all the databases within his specified privileges.
  • Comment: The user gave a remark to connect to this order, and this is an optional field of listDatabase command.

Examples of MongoDB List Databases

Given below are the examples of MongoDB List Databases:

But, first, we need to create a different database by using the following command as follows.

Code:

use demo

Explanation:

  • In the above statement, we use the “use” command to create the new database. The final output of the above statement we illustrated by using the following screenshot as follows.

Output:

MongoDB List Databases 1

Now we created another two databases as follows.

Code:

use sample
use temp

Explanation:

  • By using the above two statements, we created two new databases named sample and temp.
  • The final output of the above statement we illustrated by using the following screenshot as follows.

Output:

created another two databases

Now let’s see how we can list the database as follows.

For execution, we use the MongoDB command-line tool. Now run the following command to list all databases as follows.

Code:

show dbs;

Explanation:

  • In the above command, we use the show dbs command to list the all databases in the MongoDB server; here, dbs is used to list the database.
  • The final output of the above statement we illustrated by using the following screenshot as follows.

Output:

MongoDB List Databases 3

Now let’s see some other commands of the list database as follows.

Code:

db.adminCommand( { listDatabases: 1 } )

Explanation:

  • In the above statement, we try to run the listDatabase command against the admin database as shown. Using this statement, we can list all existing database names and sizes as per our requirements.
  • One more important thing here we include the 1; here, we use 1 to maintain the stable output of the above statement. The final output of the above statement we illustrated by using the following screenshot as follows.

Output:

other commands

Without one, we also get the same output, but some show the wrong format; that wise, we recommend using 1.

Code:

db.adminCommand('listDatabases')

Explanation:

  • By using the above statement, we try to implement the list database commands the same as the above example. Here the only difference is that we remove the 1 from the statement till we get the same result.
  • The final output of the above statement we illustrated by using the following screenshot as follows.

Output:

MongoDB List Databases 5

Now let’s see how we can list the database with only names as follows.

Code:

db.adminCommand( { listDatabases: 1, nameOnly: true} )

Explanation:

  • In the above statement, we try to implement the listDatabase command against the admin database as shown, here we use listDatabase command with nameOnly field is true.
  • That means when we need to display the only database names at that time, we can use the above statement as per our requirement. The final output of the above statement we illustrated by using the following screenshot as follows.

Output:

MongoDB List Databases 6

So in this way, we can use any filter option with the listDatabase command as per our requirement.

Conclusion

From the above article, we have seen the basic syntax of ListDatabase, and we also saw different examples of ListDatabase. From this article, we have seen how and when we use MongoDB ListDatabase.

Recommended Articles

This is a guide to MongoDB List Databases. Here we discuss the introduction, how to list databases in MongoDB? and examples, respectively. You may also have a look at the following articles to learn more –

  1. MongoDB Join
  2. MongoDB findOne()
  3. MongoDB Date Query
  4. MongoDB Database
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (17 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more