I need to connect to MongoDB from my Python code, the only thing I have is a url. Per mongo URL doc I can specify database name:
mongodb://host/db_name
Now I would like to use exactly database specified from URL and don't want to parse it manually to extract name of database. But MongoClient
have no interface to access default one. Any thoughts how to manage this?
Default created database of MongoDB is 'db' present within the data folder.
MongoDB uses TCP as its transport layer protocol. The predefined default port for connection is 27017.
Click on “Overview” tab in the menu bar. Scroll down the Overview page and you will see the MongoDB URI information.
To open up the MongoDB shell, run the mongo command from your server prompt. By default, the mongo command opens a shell connected to a locally-installed MongoDB instance running on port 27017 . Try running the mongo command with no additional parameters: mongo.
PyMongo/MongoClient does (now) provide a get_default_database()
method:
from pymongo import MongoClient
client = MongoClient("mongodb://host/db_name")
db = client.get_default_database()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With