Pymongo-未在DB上授权执行命令

发布于 2025-02-06 20:08:30 字数 1592 浏览 0 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃酥萝莉 2025-02-13 20:08:30

我找到了答案。我真的不知道有什么区别,但是就我而言,我必须创建一个函数来获得光标。

def get_database():

    from pymongo import MongoClient
    import pymongo

    # Provide the mongodb url to connect python to mongodb using pymongo
    CONNECTION_STRING = "mongodb://user:password@cluster:111111/db?ssl=true&connectTimeoutMS=10000&authSource=db&authMechanism=SCRAM-SHA-1"

    # Create a connection using MongoClient. 
    from pymongo import MongoClient
    client = MongoClient(CONNECTION_STRING)

    # Create the database
    return client['db']

善后使用此功能

dbname = get_database()

collection_name = dbname["collection_name"]

item_details = collection_name.find(condition)
for item in item_details:
    print(item)

使用此方法创建光标,一切正常。

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.

def get_database():

    from pymongo import MongoClient
    import pymongo

    # Provide the mongodb url to connect python to mongodb using pymongo
    CONNECTION_STRING = "mongodb://user:password@cluster:111111/db?ssl=true&connectTimeoutMS=10000&authSource=db&authMechanism=SCRAM-SHA-1"

    # Create a connection using MongoClient. 
    from pymongo import MongoClient
    client = MongoClient(CONNECTION_STRING)

    # Create the database
    return client['db']

and afterwads use this funcion to create cursor

dbname = get_database()

collection_name = dbname["collection_name"]

item_details = collection_name.find(condition)
for item in item_details:
    print(item)

Using this method everything works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文