2016-11-10 95 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 />"; 
} 

?>