0
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.

Paul White
95.4k30 gold badges440 silver badges689 bronze badges
asked Jun 6, 2021 at 8:54
0

2 Answers 2

1

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.

answered Jun 6, 2021 at 12:03
2
  • i dont know how i can convert this in python Commented Jun 6, 2021 at 12:32
  • It's a simple loop over an array. Check kb.objectrocket.com/mongo-db/… Commented Jun 6, 2021 at 20:15
0

You almost had it... This works:

s = myclient.admin.command('usersInfo', {'forAllDBs': True})
print(s)

With assistance from the docs for command().

answered Oct 20, 2021 at 18:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.