2

我需要列出所有可以通过 PHP 访问 MongoDB 的用户。基本上与您从 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"
        ]
}

我尝试过使用 PHP 内置的执行命令,但没有任何运气。有人有想法么?

4

3 回答 3

1

您想像这样使用system.users我相信的集合(http://docs.mongodb.org/manual/reference/privilege-documents/):

foreach($db->selectCollection('system.users')->find() as $user)
    var_dump($user);
于 2013-08-12T07:11:23.813 回答
0

它只显示第一个用户。没有列出所有这些。例如:

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

输出

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

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

)

而不是数据库上的所有用户。

于 2013-08-12T15:31:56.327 回答
0

对于任何我不得不打破命令的东西。这很好用:

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

foreach ($users as $user) {
    var_dump($user);
}
于 2013-08-12T15:35:37.153 回答