2

I need to list all the users who have access to a MongoDB, but via PHP. Basically the same as if you were do the following from the mongo shell:

> use test
switched to db test
> show users
{
        "_id" : ObjectId("51f74b1c9d8036288ae01e98"),
        "user" : "admin",
        "pwd" : "cc4fa5214a55925052f2892be6feb470",
        "roles" : [
                "readWrite",
                "dbAdmin"
        ]
}
{
        "_id" : ObjectId("52085c1dc9f60326cb20cb37"),
        "user" : "user_name",
        "pwd" : "ec416be9a5e17fbe48a7ceb93e63ecba",
        "roles" : [
                "readWrite",
                "dbAdmin"
        ]
}
{
        "_id" : ObjectId("52085c35c9f60326cb20cb38"),
        "user" : "test_user",
        "pwd" : "bf7a0adf9822a3379d6dfb1ebd38b92e",
        "roles" : [
                "readWrite"
        ]
}

I have tried using the execute command builtin to PHP without any luck. Anyone have any ideas?

3 Answers 3

1

You want to use the system.users collection I believe ( http://docs.mongodb.org/manual/reference/privilege-documents/ ) like so:

foreach($db->selectCollection('system.users')->find() as $user)
    var_dump($user);
0
0

It is only showing the first user. Not listing all of them. For example:

$db_name = "test"
echo "Database Name: $db_name <br />";
foreach($conn->$db_name->selectCollection('system.users')->find() as $users);
var_dump($users);

Outputs

Database Name: test <br />
Array
(
    [_id] => MongoId Object
        (
            [$id] => 52085c35c9f60326cb20cb38
        )

    [user] => test_user
    [pwd] => bf7a0adf9822a3379d6dfb1ebd38b92e
    [roles] => Array
        (
            [0] => readWrite
        )

)

Instead of all the users on the database.

0
0

For whatever I had to break out the command more. This works great:

$users = $conn->$db_name->selectCollection('system.users')->find();

foreach ($users as $user) {
    var_dump($user);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.