Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pymongo default database connection

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?

like image 559
Dewfy Avatar asked Jul 18 '13 10:07

Dewfy


People also ask

What is the default database in MongoDB?

Default created database of MongoDB is 'db' present within the data folder.

What is the default port of MongoDB?

MongoDB uses TCP as its transport layer protocol. The predefined default port for connection is 27017.

What is my MongoDB URL?

Click on “Overview” tab in the menu bar. Scroll down the Overview page and you will see the MongoDB URI information.

How do I access MongoDB database?

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.


1 Answers

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()
like image 164
Dan Gayle Avatar answered Sep 21 '22 06:09

Dan Gayle