PyMongo – How to List Database Names?

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.

Example 1: List Databases present in MongoDB

import pymongo

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

for db in myclient.list_databases():