在 PHP 中连接远程 MongoDB 以获得 collection 中的结果
Connecting Remote MongoDB in PHP to get the results in collection
嗨,谁能帮我配置 PHP 中的 mongodb,详细信息如下,
我已尝试连接以下 MongoServer 详细信息并从 collection 获取结果,但我收到如下所示的错误
Server : test.server.com
Port : 27017
UserName : userdb
Password : passworddb!
Authentication DB : test-db
Collection Name : MESSAGES
我试过下面的PHP代码来获取collection
的详细信息
<?
echo "<pre>";
$mongo = new MongoClient("mongodb://userdb:passworddb!@test.server.com:27017/test-db?readPreference=primary");
$dbname = "test-db";
var_dump($mongo);
$db = $mongo->$dbname;
var_dump($db);
$cursor = $db->MESSAGES->find();
foreach($cursor as $value){
var_dump($value);
}
?>
但我收到类似
的错误
object(MongoClient)#1 (4) {
["connected"]=>
bool(true)
["status"]=>
NULL
["server":protected]=>
NULL
["persistent":protected]=>
NULL
}
object(MongoDB)#3 (2) {
["w"]=>
int(1)
["wtimeout"]=>
int(10000)
}
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: test.server.com:27017: SASL Authentication failed on database 'test-db': Authentication failed.' in C:\xampp\htdocs\angular\mongo.php:22
Stack trace:
#0 C:\xampp\htdocs\angular\mongo.php(22): MongoCursor->rewind()
#1 {main}
thrown in C:\xampp\htdocs\angular\mongo.php on line 22
第22行参考foreach($cursor as $value){
我通过更改代码找到了解决方案,如下所示。现在我可以获取集合中的所有记录了。
<?
$mongo=new MongoClient("mongodb://xhomuser:passworddb!@test.server.com:27017/test-db?readPreference=primary");
echo "<pre>";
$db = $mongo->selectDB("test-db");
$collection = $db->selectCollection("MESSAGES");
$cursor = $collection->find();
foreach ($cursor as $document) {
echo '"_id": '.$document["_id"]."<br />";
echo '"accountnumber": '.$document["ACCOUNTNUMBER"]."<br />";
echo '"STATUS": '.$document["STATUS"]."<br />";
}
?>
嗨,谁能帮我配置 PHP 中的 mongodb,详细信息如下,
我已尝试连接以下 MongoServer 详细信息并从 collection 获取结果,但我收到如下所示的错误
Server : test.server.com
Port : 27017
UserName : userdb
Password : passworddb!
Authentication DB : test-db
Collection Name : MESSAGES
我试过下面的PHP代码来获取collection
的详细信息 <?
echo "<pre>";
$mongo = new MongoClient("mongodb://userdb:passworddb!@test.server.com:27017/test-db?readPreference=primary");
$dbname = "test-db";
var_dump($mongo);
$db = $mongo->$dbname;
var_dump($db);
$cursor = $db->MESSAGES->find();
foreach($cursor as $value){
var_dump($value);
}
?>
但我收到类似
的错误object(MongoClient)#1 (4) {
["connected"]=>
bool(true)
["status"]=>
NULL
["server":protected]=>
NULL
["persistent":protected]=>
NULL
}
object(MongoDB)#3 (2) {
["w"]=>
int(1)
["wtimeout"]=>
int(10000)
}
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: test.server.com:27017: SASL Authentication failed on database 'test-db': Authentication failed.' in C:\xampp\htdocs\angular\mongo.php:22
Stack trace:
#0 C:\xampp\htdocs\angular\mongo.php(22): MongoCursor->rewind()
#1 {main}
thrown in C:\xampp\htdocs\angular\mongo.php on line 22
第22行参考foreach($cursor as $value){
我通过更改代码找到了解决方案,如下所示。现在我可以获取集合中的所有记录了。
<?
$mongo=new MongoClient("mongodb://xhomuser:passworddb!@test.server.com:27017/test-db?readPreference=primary");
echo "<pre>";
$db = $mongo->selectDB("test-db");
$collection = $db->selectCollection("MESSAGES");
$cursor = $collection->find();
foreach ($cursor as $document) {
echo '"_id": '.$document["_id"]."<br />";
echo '"accountnumber": '.$document["ACCOUNTNUMBER"]."<br />";
echo '"STATUS": '.$document["STATUS"]."<br />";
}
?>