Find all databases which a specific user is a member/owner of

70 views
Skip to first unread message

Michael Ward

unread,
Dec 8, 2015, 6:11:05 PM12/8/15
to mongodb-user
Hey there,

Setting up a dedicated MongoDB server for our students.  Is there an easy way to ask mongod, "give me a list of all databases that user 'abc' is a member of"?  From what I can tell it seems like I would need to list out all existing databases with "show dbs" then "db.getUsers()" and look through each one to find the user.

I'm planning on writing my administrative scripts with Python, so I've got my eye on PyMongo, though that's not an absolute requirement if there's an easier way.

Thanks!
Mike

Wan Bachtiar

unread,
Dec 9, 2015, 12:14:27 AM12/9/15
to mongodb-user

Is there an easy way to ask mongod, “give me a list of all databases that user ‘abc’ is a member of”?

Hi Michael,

If you have access to the userAdmin or userAdminAnyDatabase role for the admin database, you should be able to run find() in system.users collection.

In Mongo Shell, an example query to list of all databases user abc having a role(s) in:

use admin; 
db.system.users.find(
    /* Filter user 'abc' only */
    {user:"abc"}, 
    /* Project only db names in roles */
    {"roles.db":1}
);

You may want to get the role for the related database as well for completeness. As a user can be listed to have an involvement in database 'foo' but may not have write access to it. 

You may find the list of the available built-in roles useful. 

Best regards,

Wan.

Michael Ward

unread,
Dec 9, 2015, 11:33:20 AM12/9/15
to mongodb-user
Thank you very much, Wan!  Clearly I have much to learn about Mongo.  Much appreciated.

Mike
Reply all
Reply to author
Forward
0 new messages