2 / 4
May 2024

I am using MongoSH and PyMongo within Visual Code on WSL. I have successfully accessed the cluster from MongoSH, but PyMongo seems to be successfully accessing some cluster but one that doesn’t have my username.
For the following code in PyMongo:
print(db.command({ ‘connectionStatus’: 1 }))
The response is:
{‘authInfo’: {‘authenticatedUsers’: , ‘authenticatedUserRoles’: }, ‘ok’: 1.0},
but for
db.runCommand({ connectionStatus: 1 }) in MongoSH, I get
{
authInfo: {
authenticatedUsers: [ { user: ‘natanmelzer’, db: ‘admin’ } ],
authenticatedUserRoles: [ { role: ‘readWriteAnyDatabase’, db: ‘admin’ } ]
},
ok: 1
}
How is this possible, and how can I fix it?

after

client=MongoClient('mongodb+srv://yourname:yourpassword@yourcluster.mongodb.net'

(or however you get your MongoClient instance)

db=client.foo print(db.command({'connectionStatus': 1}))

works for me.

This helped me snap out of trying to run PyMongo using the Shell, and got me to just use the connection string in PyMongo, which I should have done all along.