通过python中的pymongo进行Mongo DB连接

时间:2018-11-26 08:35:33

标签: python mongodb pymongo

我是MongoDB的新手,并尝试实现用于通过Python连接mongo的代码。我使用了标准python用户手册中的pymongo软件包。并为此选择mlab。

from pymongo import MongoClient
client = MongoClient('mongodb://test:test@account.mlab.com:port',
                     connectTimeoutMS=300000,maxPoolSize=50)

print(client)
  

MongoClient(host = ['account.mlab.com:port'],               document_class = dict,tz_aware = False, connect = True ,maxpoolsize = 50,connecttimeoutms = 300000)

很明显,我可以看到连接成功。现在,让我们检查数据库

db=client.get_database(test) // Specifying a DB

print(db)

  

数据库(MongoClient(主机= ['account.mlab.com:port'],                        document_class = dict,tz_aware = False,                         connect = True ,connecttimeoutms = 300000), u'test'

确认与所选测试数据库的连接成功。

users = db.users // Selecting a collection
print(users)
  

Collection(Database(MongoClient(host = ['ds024778.mlab.com:24778'],                                   document_class = dict,tz_aware = False,                                    connect = True ,connecttimeoutms = 300000), u'test '), u'users'

现在,我正在尝试向username = 'admin'

的用户获取文档
r = users.find_one({"username":'admin'})

但是,错误

OperationFailure: Authentication failed.

0 个答案:

没有答案