MongoDB Create DB
Selecting a MongoDB database name creates a database handle. Storage is normally created only after an actual write or explicit collection creation.
Understand the idea
This distinction matters because listing database names immediately after selecting one may not show the new name. The example writes one fixed lab marker with upsert so rerunning does not create duplicate setup markers. Use a separate database name dedicated to these lessons rather than a production application database.
Where to try it
Use a local Python environment with pymongo and your own separate MongoDB lab server. Set the lab connection through the environment, as shown below. The online runner does not provide this server connection.
A worked example
import os
from pymongo import MongoClient
with MongoClient(os.environ["MONGODB_URI"], serverSelectionTimeoutMS=3000) as client:
database = client["bhylu_python_lab"]
collection = database["workshop_products"]
marker = database["lab_metadata"]
marker.update_one({"_id": "setup"}, {"$set": {"course": "Python"}}, upsert=True)
print(database.name)
Read the result
The lab database name is printed after a real write establishes content.
Try one change
Select another lab handle without writing and compare what appears in the server's database list.
Local MongoDB lab
Install pymongo in your local virtual environment and run a separate MongoDB lab server. Set MONGODB_URI in the environment of the Python process. These examples use bhylu_python_lab and workshop_products; create a few lab products before read/update lessons. Network and server operations are not available in the restricted online runner. The snippets are syntax-checked, not claimed to have been executed against your server.