⊕
⊖
Contents
PyMongo List Database Names
To list databases present in a MongoDB instance,
- Create a client to the MongoDB instance.
- Using the client, call list_databases() function.
- 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)
CopyOutput
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.