Pymongo运行mongodb shell命令getusers()

发布于 2025-02-09 17:22:52 字数 120 浏览 1 评论 0原文

我正在使用pymongo。我需要在pymongo中运行db.getusers()。是否有任何文档可以这样做。我只看到了Pymongo命令。似乎不会使用pymongo运行蒙古命令并检索数据,

如果有人知道,请张贴我

I am using pymongo . I need to run db.getUsers() in pymongo. Is there any documentations to do so. I only saw the pymongo commands . There doesn't seems to run mongoshell commands using pymongo and retrieve data

If any one know please post me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

撩动你心 2025-02-16 17:22:52

这是用于将pymongo与mongodb连接并进行基本操作的锅炉板代码

from pymongo import MongoClient

def data_connection():
    url="url of your mongodb instance" ##in case of localhost it will be "localhost"
    conn = MongoClient(url)
    db = conn["admin"] ##name of the database
    return db


db = data_connection()
collection = db.Users
res = list(collection.find({})) ##your query will be like this

This is the Boiler Plate code for connecting Pymongo with Mongodb and doing basic operation

from pymongo import MongoClient

def data_connection():
    url="url of your mongodb instance" ##in case of localhost it will be "localhost"
    conn = MongoClient(url)
    db = conn["admin"] ##name of the database
    return db


db = data_connection()
collection = db.Users
res = list(collection.find({})) ##your query will be like this
荒人说梦 2025-02-16 17:22:52

现在,我将用户和凭据变成了以下内容,

db=client['admin'] 
collection=db.system.users
data=collection.find_one({"user" : 'test_user'})   

并且数据具有以下内容,

{'_id': 'admin.test_user',
 'credentials': {'SCRAM-SHA-1': {'iterationCount': 10000,
                                 'salt': '5MwJVYNXO0AVeLQgkO12YA==',
                                 'serverKey': 'zB4lE4/TLS44YfRGzf9fH9jJ+Vk=',
                                 'storedKey': '6p0SJvHJXtHiNWbc3eJLZhootW8='},
                 'SCRAM-SHA-256': {'iterationCount': 15000,
                                   'salt': 'J9jHjDNrCzLrr/U2Qb6Ei6WIRpDBDYIJVRiEfw==',
                                   'serverKey': 'Ej6xiz3uwf2ScGlMvQi+lqoJQLjUCGYAcJXh9kxo8iw=',
                                   'storedKey': '9y0MoPBLLjgepc8TfeakFWMoNObWHeH//sJInRLYy9Q='}},
 'db': 'admin',
 'roles': [{'db': 'test_db', 'role': 'dbOwner'}],
 'user': 'test_user',
 'userId': Binary(b'<\xb7\xbf\xa0\xd0\xa7D\xdb\xad\x1aY\xc5n\xd7\x1d\xa4', 4)}

下一个问题是如何将这些数据还原到System.user。

Now I got the user and credentials into a dict as follows,

db=client['admin'] 
collection=db.system.users
data=collection.find_one({"user" : 'test_user'})   

And the data have the following,

{'_id': 'admin.test_user',
 'credentials': {'SCRAM-SHA-1': {'iterationCount': 10000,
                                 'salt': '5MwJVYNXO0AVeLQgkO12YA==',
                                 'serverKey': 'zB4lE4/TLS44YfRGzf9fH9jJ+Vk=',
                                 'storedKey': '6p0SJvHJXtHiNWbc3eJLZhootW8='},
                 'SCRAM-SHA-256': {'iterationCount': 15000,
                                   'salt': 'J9jHjDNrCzLrr/U2Qb6Ei6WIRpDBDYIJVRiEfw==',
                                   'serverKey': 'Ej6xiz3uwf2ScGlMvQi+lqoJQLjUCGYAcJXh9kxo8iw=',
                                   'storedKey': '9y0MoPBLLjgepc8TfeakFWMoNObWHeH//sJInRLYy9Q='}},
 'db': 'admin',
 'roles': [{'db': 'test_db', 'role': 'dbOwner'}],
 'user': 'test_user',
 'userId': Binary(b'<\xb7\xbf\xa0\xd0\xa7D\xdb\xad\x1aY\xc5n\xd7\x1d\xa4', 4)}

The next question is how can I restore this data into System.users

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