MongoDB\Driver\Cursor::toArray

(mongodb >=1.0.0)

MongoDB\Driver\Cursor::toArrayReturns an array containing all results for this cursor

说明

final public MongoDB\Driver\Cursor::toArray ( void ) : array

Iterates the cursor and returns its results in an array. MongoDB\Driver\Cursor::setTypeMap() may be used to control how documents are unserialized into PHP values.

参数

此函数没有参数。

返回值

Returns an array containing all results for this cursor.

错误/异常

范例

Example #1 MongoDB\Driver\Cursor::toArray() example

<?php

$manager 
= new MongoDB\Driver\Manager("mongodb://localhost:27017");

$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1]);
$bulk->insert(['x' => 2]);
$bulk->insert(['x' => 3]);
$manager->executeBulkWrite('db.collection'$bulk);

$query = new MongoDB\Driver\Query([]);
$cursor $manager->executeQuery('db.collection'$query);

var_dump($cursor->toArray());

?>

以上例程的输出类似于:

array(3) {
  [0]=>
  object(stdClass)#6 (2) {
    ["_id"]=>
    object(MongoDB\BSON\ObjectId)#5 (1) {
      ["oid"]=>
      string(24) "564259a96118fd40b41bcf61"
    }
    ["x"]=>
    int(1)
  }
  [1]=>
  object(stdClass)#8 (2) {
    ["_id"]=>
    object(MongoDB\BSON\ObjectId)#7 (1) {
      ["oid"]=>
      string(24) "564259a96118fd40b41bcf62"
    }
    ["x"]=>
    int(2)
  }
  [2]=>
  object(stdClass)#10 (2) {
    ["_id"]=>
    object(MongoDB\BSON\ObjectId)#9 (1) {
      ["oid"]=>
      string(24) "564259a96118fd40b41bcf63"
    }
    ["x"]=>
    int(3)
  }
}

参见

相关文章
php mongodb driver cursor returns the id for this cursorphp mongodb driver cursor returns the server associated with this cursorphp mongodb driver cursor checks if the cursor may have additional resultsphp mongodb driver cursor returns an array containing all results for this cursorphp mongodb driver monitoring commandfailedevent returns the server on which the command was executedphp mongodb driver monitoring commandstartedevent returns the command documentphp mongodb driver monitoring commandstartedevent returns the command namephp mongodb driver monitoring commandstartedevent returns the database on which the command was executedphp mongodb driver readconcern returns an object for bson serializationphp mongodb driver readconcern returns the readconcern s level optionphp mongodb driver server returns the hostname of this serverphp mongodb driver server returns an array of information about this serverphp mongodb driver server returns an array of tags describing this server in a replica setphp mongodb driver writeconcern returns the writeconcern s w optionphp mongodb driver writeconcern returns the writeconcern s wtimeout optionphp mongodb driver writeconcernerror returns the writeconcernerror s error codephp mongodb driver writeresult returns the number of documents inserted by an upsertphp mongodb driver writeresult returns an array of identifiers for upserted documentsphp mongodb driver writeresult returns any write concern error that occurredphp solrinputdocument returns an array containing all the fields in the document
关注编程学问公众号