Connecting to mongodb using Python API and username:password

3,440 views
Skip to first unread message

Sandra Arnold

unread,
May 16, 2013, 10:43:49 AM5/16/13
to mongod...@googlegroups.com
I was looking at the documentation for connecting to a MongoDB using the Python API and cannot find the correct syntax to connect when a username and password is required to connect.  Can you please point me to documentation that would have an example?
 
Thanks,

Sandra Arnold

Jeff Lee

unread,
May 16, 2013, 11:42:47 AM5/16/13
to mongod...@googlegroups.com
Sandra,

Authentication is performed at the database level, so something like:

>>> import pymongo
>>> c = pymongo.Connection()
>>> username = 'theuser'
>>> password = 'mypssword'
>>> c['admin'].authenticate( username, password)
True
>>> c['admin'].system.users.find_one({},{'user':1})
{u'_id': ObjectId('5194fd272993fb506a05d5b0'), u'user': u'theuser'}



--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Bernie Hackett

unread,
May 16, 2013, 12:54:08 PM5/16/13
to mongod...@googlegroups.com
You can also pass credentials in the URI:

c = pymongo.MongoClient(uri)

Sandra Arnold

unread,
May 16, 2013, 2:30:36 PM5/16/13
to mongod...@googlegroups.com
Thanks.  As soon as we get Tornado and Phython installed on the server, I will attempt to connect using what you have posted.  In the meantime, I plan to document in my Procedures that I am developing for MongoDB.
Reply all
Reply to author
Forward
0 new messages