Pymongo-未在DB上授权执行命令
我在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"
]
}
,这是
Connection_string= 'mongodb://user:password@cluster:111111/db?ssl=false&connectTimeoutMS=10000&authSource=db&authMechanism=SCRAM-SHA-1'
我在Studio 3T中使用此连接字符串的连接字符串,我在查询数据时没有问题。但是,当我尝试在Pymongo中使用它时,我会遇到错误。这是我的代码
from pymongo import MongoClient
client = MongoClient(Connection_string)
db=client.db
collection = db.collection_name
coursor= collection.find({})
for r in coursor:
print (r)
,这是一条错误消息,
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}}}
我正在检查stackoverflow中有关此问题的一些线程,但是,其中不适合我的情况。我只想查询创建我使用的用户的数据库
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了答案。我真的不知道有什么区别,但是就我而言,我必须创建一个函数来获得光标。
善后使用此功能
使用此方法创建光标,一切正常。
I have found an answer. I don't really know what is a difference, but in my case I had to create a function to get cursor.
and afterwads use this funcion to create cursor
Using this method everything works fine.