简体   繁体   中英

Pymongo throws “KeyError: 'errmsg'” when attempting to connect to mongodb

I am using the following code to connect to 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)

I receive this error:

errmsg - details["errmsg"]
KeyError: 'errmsg'

This doesn't really tell me much, so I'm having trouble figuring out what's wrong. This code stems from tutorials such as this one https://scotch.io/tutorials/getting-started-with-python-and-mongodb .

Try this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM