db.command('usersInfo')
The above command gives the list of users in the database in which the command is run.
command_result = db.command(
{
'usersInfo': {'user': 'user', 'db': 'mydatabase'},
'showPrivileges': True
} )
The above code gives information about the users in the 'mydatabase' database.
But I want to list all the users in all the databases. Can anyone tell me what command I can use in python for that?
s = myclient.admin.command({'usersInfo' :{'forAllDBs: true'}}) print(s)
When I am using above command, I am getting an error. I am using pymongo
.
2 Answers 2
You can try this one:
for (let d of db.adminCommand( { listDatabases: 1 } ).databases ) {
db.getSiblingDB(d.name).getUsers()
}
This is javascript code in mongo shell, should be no big issue to convert it into python.
-
i dont know how i can convert this in pythonAcmakala Eesha– Acmakala Eesha2021年06月06日 12:32:03 +00:00Commented Jun 6, 2021 at 12:32
-
It's a simple loop over an array. Check kb.objectrocket.com/mongo-db/…Wernfried Domscheit– Wernfried Domscheit2021年06月06日 20:15:23 +00:00Commented Jun 6, 2021 at 20:15
You almost had it... This works:
s = myclient.admin.command('usersInfo', {'forAllDBs': True})
print(s)
With assistance from the docs for command().