如何使用pymongo与mongo URI建立正确的连接?

时间:2016-04-28 11:57:40

标签: mongodb

我不知道如何通过URI正确连接mongodbs与pymongo(版本> 3)。两种类型的脚本列表如下所示,类型B可以正确运行.find(),但类型A不能。如何修改类型A中的代码?

输入A:

uri = "mongodb://{}:{}@{}:{}".format(username, password, host, port)
for k in ["A","B","C"]:
    exec "{}_uri = '{}/{}_{}'".format(k, uri, k, envname)
    exec "db_{} = MongoClient({}_uri)".format(k, k)

B型:

connection = MongoClient(host, port)
for k in ["A","B","C"]:
    exec "db_{} = connection.{}_{}".format(k, k, envname)
    exec "db_{}.authenticate(username, password)".format(k)

0 个答案:

没有答案