I have this user in MongoDB
{
"_id" : "xxx",
"userId" : UUID("xxx"),
"user" : "user",
"db" : "db",
"roles" : [
{
"role" : "dbOwner",
"db" : "db"
},
{
"role" : "readWrite",
"db" : "db"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
and this is my connection string
Connection_string= 'mongodb://user:password@cluster:111111/db?ssl=false&connectTimeoutMS=10000&authSource=db&authMechanism=SCRAM-SHA-1'
When I am using this connection string in the Studio 3T, I have no issues querying data. However, when I try to use it in pymongo I am getting an error. This is my code
from pymongo import MongoClient
client = MongoClient(Connection_string)
db=client.db
collection = db.collection_name
coursor= collection.find({})
for r in coursor:
print (r)
And this is an error message
OperationFailure: not authorized on db to execute command { find: "collection_name", filter: {}, lsid: { id: UUID("xx") }, $clusterTime: { clusterTime: Timestamp(1654866078, 1), signature: { hash: BinData(0, xx), keyId: xx} }, $db: "db" }, full error: {'operationTime': Timestamp(1654866078, 1), 'ok': 0.0, 'errmsg': 'not authorized on db to execute command { find: "collection_name", filter: {}, lsid: { id: UUID("xx") }, $clusterTime: { clusterTime: Timestamp(1654866078, 1), signature: { hash: BinData(0, xx), keyId: xx} }, $db: "db" }', 'code': 13, 'codeName': 'Unauthorized', '$clusterTime': {'clusterTime': Timestamp(1654866078, 1), 'signature': {'hash': b'xxxx', 'keyId': xx}}}
I was checking some threads regarding this issue in StackOverflow, however, non of them suit my situation. I want to query only the DB in which the user I am using is created