Pymongo在尝试连接到mongodb时抛出“KeyError:'errmsg'”

2024-04-25 06:45:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用以下代码连接到mongo:

full = "mongodb+srv://myuser:mypass@hpechatops-dzlpw.mongodb.net/test?retryWrites=true"
client = MongoClient(full)
db = client.admin

serverStatusResult=db.command("serverStatus")
pprint(serverStatusResult)

我收到这个错误:

^{2}$

这并不能告诉我太多,所以我很难弄清楚到底是怎么回事。这段代码源于教程,如https://scotch.io/tutorials/getting-started-with-python-and-mongodb。在


Tags: 代码testclientdbnetmongomongodbfull
1条回答
网友
1楼 · 发布于 2024-04-25 06:45:11

试试这个:

from pymongo import MongoClient

mongo_host = "(enter the prefix).mlab.com"
mongo_port = (enter port number)
mongo_db = "(enter db name)"
mongo_user = "(enter username)"
mongo_pass = "(enter password)"
connection = MongoClient(mongo_host, mongo_port)
db = connection[mongo_db]
db.authenticate(mongo_user, mongo_pass)

相关问题 更多 >