On the command line, this works:
$ mongo
> show dbs
mydatabase 1.0GB
However, this does not:
$ python
>>> import pymongo
>>> connection = pymongo.MongoClient()
>>> connection.mydatabase.find()
I read through docs here:
http://api.mongodb.org/python/current/tutorial.html
But do not understand how to either...
- connect to an existing database (using pymongo)
- query what databases exist in the mongodb connection.
Why can't I access my database?
show dbs
andfind()
are totally different commands as such you cannot compare the two.Will actually do nothing because you cannot
find()
documents on database level. You are probably looking for:I am no Python programmer but something like that and the
foreach
thecursor
var to get your data.As an added note you will want to replace
mycol
with the collection name that contains your documents.As for querying for a list of databases you can do something like:
As shown here: http://docs.mongodb.org/manual/reference/command/listDatabases/#listDatabases
However again I am no Python programmer but this should get you started.
Connect to an existing database
List existing databases
The question implies user has a local MongoDB. However I found this question trying to connect to a remote MongoDB. I think the tutorial is worth mentioning (no other answer here mentioned how I can specify the host and the port)
client = MongoClient('localhost', 27017)
client = MongoClient('mongodb://localhost:27017/')
On the python command line:
Although there doesn't seem to be a wealth of examples, it appears that the bindings are pretty complete within the Python Driver API Documentation.