Connect to Mongodb in python

Posted by SpawnCxy on Stack Overflow See other posts from Stack Overflow or by SpawnCxy
Published on 2010-03-29T03:36:28Z Indexed on 2010/03/29 3:43 UTC
Read the original article Hit count: 354

Filed under:
|
|

I'm a little confused by the document when I tried to connect to the Mongodb.And I find it's different from mysql.I want to create a new database named "mydb" and insert some posts into it.The follows is what I'm trying.

from pymongo.connection import Connection
import datetime

host = 'localhost'
port = 27017
user = 'ucenter'
passwd = '123'

connection = Connection(host,port)
db = connection['mydb']

post = {'author':'mike',
    'text':'my first blog post!',
    'tags':['mongodb','python','pymongo'],
    'date':datetime.datetime.utcnow()}

posts = db.posts
posts.insert(post)

#print str(db.collection_names())

And I got an error as pymongo.errors.OperationFailure: database error: unauthorized.How can I do the authorizing part?Thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about mongodb