1

fields()カーソルでメソッドを使用してみました:

<?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);

出力は次のとおりです。

Mongo Object ( [connected] => 1 [status] => [server:protected] => [persistent:protected] => )
MongoCursor Object ( )
MongoCursor Object ( ) 

ドライバーと接続は機能しているように見えますが、フィールドの明確な要約を取得できません。

4

3 に答える 3

-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
}
于 2013-07-20T11:26:15.707 に答える
-2

$cursor 変数の後、代わりに foreach を試してください。カーソルの結果を通過します。

foreach($cursor as $document) {  
 var_dump($document);  
}
于 2013-07-15T11:24:27.230 に答える