2016-11-10 97 views
0

嗨任何人都可以与我帮助配置PHP中的mongodb下面详细的结果,在PHP连接远程MongoDB中获得的收集

我试图连接与下面MongoServer细节,并从集合得到的结果,但我得到的错误如下图所示

Server : test.server.com 
Port : 27017 
UserName : userdb 
Password : passworddb! 
Authentication DB : test-db 
Collection Name : MESSAGES 

我曾尝试以下PHP代码获取集合

<? 
     echo "<pre>"; 
    $mongo = new MongoClient("mongodb://userdb:[email protected]: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); 
    } 
    ?> 

的细节,但我摹ETTING错误等

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个至Referes的foreach ($光标为$值){

回答

0

我通过改变如下代码中发现的该溶液中。现在我可以获取集合中的所有记录。

<? 
$mongo=new MongoClient("mongodb://xhomuser:[email protected]: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 />"; 
} 

?>