skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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'<xb7xbfxa0xd0xa7Dxdbxadx1aYxc5nxd7x1dxa4', 4)}
    

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


  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search