1

I tried using the fields() method on the cursor:

<?php
$mongo  = new Mongo("mongodb://localhost");
print_r($mongo);

$db = $mongo->test;

// access collection
$collection = $db->test;

// execute query
// retrieve all 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 like the driver and the connection work but I can't retrieve the distinct summarization of the fields.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151

3 Answers3

0

Assume that we have access to mongodb database with the name db, are going to retrieve data from MyCollection and use MongoDB\Driver\Manager:

  $manager = new MongoDB\Driver\Manager( $DB_CONNECTION_STRING );

Then we retrieve data in this case by name with a limit 2 documents and want to get only fields name, age and address among many others. Projection option can be used to specify which fields should be returned using 0 to exclude and 1 to include:

  $filter = ['name' => 'John'];
  $options = [ 'projection' => ['_id' => 0, 'name' => 1, 'age' => 1, 'address' => 1], 'limit' => 2 ];
  $query = new MongoDB\Driver\Query($filter, $options);
  $manager->executeQuery('db.MyCollection', $query);

If we need to get all columns, then we simply omit projection option at all as the following:

  $filter = ['name' => 'daniel'];
  $options = [];
  $query = new MongoDB\Driver\Query($filter, $options);
  $manager->executeQuery('db.MyCollection', $query);
Roman
  • 19,236
  • 15
  • 93
  • 97
-1
 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 to find One 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
}
Ali ZahediGol
  • 876
  • 2
  • 10
  • 20
-2

After the $cursor variable try an foreach instead. It goes through the cursor results.

foreach($cursor as $document) {  
 var_dump($document);  
}
foxtrot
  • 1,056
  • 1
  • 8
  • 24
  • this works to fetch all rows (works for `find()`), I guess there is no way to get all distinct fields available in a collection? – Daniel W. Jul 15 '13 at 11:53
  • See this http://stackoverflow.com/questions/5236160/mongo-equivalent-of-sqls-select-distinct – foxtrot Jul 16 '13 at 13:42
  • This fetches distinct values, but I need distinct keys :-( Like in my sample above, I need "name, randomNumber" as a result – Daniel W. Jul 16 '13 at 13:50