Home > Enterprise >  Pymongo run mongodb shell commands getUsers()
Pymongo run mongodb shell commands getUsers()

Time:06-22

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

CodePudding user response:

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

CodePudding user response:

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

  • Related