MongoDb:PHP 中的 db.getUser() [英] MongoDb: db.getUser() in PHP

查看:38
本文介绍了MongoDb:PHP 中的 db.getUser()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用 PHP 编写一个连接到 MongoDB 数据库的应用程序.

I'm currently writing an app in PHP that connects to a MongoDB database.

我已经成功地通过我设置的 MongoDB 数据库中的 dbAdmin 角色对用户进行了身份验证,并且我创建了一个用户TestUser",其中没有我想在身份验证为dbAdmin 用户.但是我不知道如何从 PHP 运行db.getUser()"命令.

I've managed to successfully authenticate a user with the dbAdmin role in the MongoDB Database that I've setup, and I've created a user "TestUser" with no roles that I want to get information about while authenticated as the dbAdmin User. however I can't figure out how to run the "db.getUser()" command from PHP.

我的理解是,我应该能够使用MongoDB::command()"方法直接运行任何实际的 mongoDB 命令,但是,以下代码...

My understanding is that I should be able to use the "MongoDB::command()" method to run any actual mongoDB commands directly, however, the following code...

$mongo = new MongoClient("localhost:27017");
$mongoDb = $mongo->{'test'}
$mongoDb->authenticate($username, $password); //This returns a successful response of array('ok' => 1)
print_r($mongoDb->command(array("getUser" => "TestUser")));

生成此响应:-

(
    [ok] => 0
    [errmsg] => no such command: getUser
    [code] => 59
    [bad cmd] => stdClass Object
        (
            [getUser] => TestUser
        )
)

getUser"是 MongoDB 中数据库的命令,对吗?如果是这样,为什么我会收到此响应,以及如何构建正确的语句以便能够在 PHP 中从 MongoDB 获取有关用户的信息?

The "getUser" is a command of a Database in MongoDB though, right? If so, why am I getting this response, and how do I construct a correct statement to be able to get information about a User from MongoDB in PHP?

推荐答案

我对此进行了更多研究,通过使用...获取数据库可用的所有可能方法的列表...

I looked into this more, by fetching a list of all possible methods available to the database by using...

$mongoDb->command(array("listCommands" => 1));

这告诉我getUser"显然不是现有命令.我不知道为什么不,我猜是因为它是一个用户管理命令而不是一个典型的数据库命令.

This informed me that "getUser" apparently is not an existing command. I'm not sure why not, I guess it's because it's a User Management command and not a typical Database Command.

无论如何,usersInfo"是一个可用的命令.通过执行以下操作...

At any rate, "usersInfo" is a command that is available. By doing the following...

$mongoDb->command(array("usersInfo" => array("user" => $username, "db" => $database)));

我能够获得有关我想要的用户的数据,所以我想这回答了我的问题.希望这可以帮助其他任何在尝试使用 MongoDb 设置 PHP 应用程序时遇到相同问题的人.

I was able to get the data about the user that I wanted, so I guess that answers my question. Hopefully this helps anyone else whose encountering the same problem trying to setup their PHP app with MongoDb.

这篇关于MongoDb:PHP 中的 db.getUser()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆