Connecting Remote MongoDB in PHP to get the results in collection -
hi can me configuring mongodb in php below details,
i have tried connecting below mongoserver details , results collections, getting error shown below
server : test.server.com port : 27017 username : userdb password : passworddb! authentication db : test-db collection name : messages
i have tried below php code details of 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 $value){ var_dump($value); } ?>
but getting error like
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' message 'failed 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
line 22 referes foreach($cursor $value){
i found solution changing code below. able fetch records in collection.
<? $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 $document) { echo '"_id": '.$document["_id"]."<br />"; echo '"accountnumber": '.$document["accountnumber"]."<br />"; echo '"status": '.$document["status"]."<br />"; } ?>
Comments
Post a Comment