List Database Names - PyMongo - Examples


PyMongo List Database Names

To list databases present in a MongoDB instance,

  1. Create a client to the MongoDB instance.
  2. Using the client, call list_databases() function.
  3. The functions returns an iterator, use for loop to iterate through the list of databases.

Examples

1. List databases present in MongoDB instance

In the following program, we connected to the MongoDB instance running locally. Then we called list_databases() on the connection object, and using a For loop, printed the database details.

Python Program

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")

for db in myclient.list_databases():
    print(db)

Output

Summary

In this PyMongo Tutorial, we learned how to get the list of databases present in a MongoDB instance using list_databases() function, with examples.