mongodb php - Retrieve columns of Mongo collection in PHP -


i tried following:

$mongo  = new mongo("mongodb://localhost"); print_r($mongo);  $db = $mongo->test;  // access collection $collection = $db->test;  // execute query // retrieve documents print_r($test);  $cursor = $collection->find(); print_r($cursor);  $fields = $cursor->fields(array("summary" => true)); print_r($fields); 

the output is:

mongo object ( [connected] => 1 [status] => [server:protected] => [persistent:protected] => ) mongocursor object ( ) mongocursor object ( )  

looks driver , connection works can't recieve columns of collection test in db test.

output of shell client:

> show dbs local   (empty) test    0.203125gb > show collections system.indexes test >  > db.test.find() { "_id" : objectid("51c861eab6a7c3caa2dc51ec"), "name" : "daniel", "randomnumber" : 49 } { "_id" : objectid("51c861f4b6a7c3caa2dc51ed"), "name" : "peter", "randomnumber" : 31 } { "_id" : objectid("51c861fab6a7c3caa2dc51ee"), "name" : "ines", "randomnumber" : 7 } { "_id" : objectid("51c86b19600090465bc233f4"), "name" : "stein", "randomnumber" : 192 } { "_id" : objectid("51c86b21600090465bc233f5"), "name" : "ziege", "randomnumber" : 42 } { "_id" : objectid("51c86b36600090465bc233f6"), "name" : "title", "randomnumber" : 753 } 

> how-to columns dynamically in php ?

 const host = 'localhost';  const port = 27017;  const dbname = 'test';  const collection = 'test';  $connectionstring = sprintf('mongodb://%s:%d', host, port);   try {          $connection = new mongo($connectionstring);         $database = $connection->selectdb(dbname);   } catch (mongoconnectionexception $e) {          throw $e;  } $collection = $database->selectcollection(collection);  try{         $query = array();         $specifykey = array("summary" => true);         $cursor = $collection->find($query , $specifykey);  }catch(mongoexception $e) {          die('failed find 1 data '.$e->getmessage());  }catch (mongocursorexception $e) {      echo "error message: ".$e->getmessage()."\n";     echo "error code: ".$e->getcode()."\n"; }  while ($cursor->hasnext()){      $nextcursor = $cursor->getnext();      //process next cursor } 

Comments