Listing databases and collections
Our MongoClient
object is not actually a dictionary, so we can't call keys()
to list the names of accessible databases. The same is true for listing collections of a database. Instead, we can list database names by calling .list_database_names()
on a client instance, and we can list collection names by calling .list_collection_names()
on a database instance.
This exercise is part of the course
Introduction to MongoDB in Python
Exercise instructions
- Save a list, called
db_names
, of the names of the databases managed by our connectedclient
. - Similarly, save a list, called
nobel_coll_names
, of the names of the collections managed by the "nobel" database.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Save a list of names of the databases managed by client
db_names = client.____()
print(db_names)
# Save a list of names of the collections managed by the "nobel" database
nobel_coll_names = client.____.____()
print(nobel_coll_names)