MongoDB\Driver\Cursor::setTypeMap

(mongodb >=1.0.0)

MongoDB\Driver\Cursor::setTypeMap โ€” Sets a type map to use for BSON unserialization

่ชฌๆ˜Ž

final public MongoDB\Driver\Cursor::setTypeMap(array $typemap): void

Sets the type map configuration to use when unserializing the BSON results into PHP values.

ใƒ‘ใƒฉใƒกใƒผใ‚ฟ

typeMap (array)

Type map configuration.

ๆˆปใ‚Šๅ€ค

ๅ€คใ‚’่ฟ”ใ—ใพใ›ใ‚“ใ€‚

ใ‚จใƒฉใƒผ / ไพ‹ๅค–

When iterating over the cursor, the following exceptions can also be thrown due to an incorrect type map configuration:

ไพ‹

ไพ‹1 MongoDB\Driver\Cursor::setTypeMap() example

<?php

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

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

$query = new MongoDB\Driver\Query(['_id' => $id]);
$cursor = $manager->executeQuery('db.collection', $query);
$cursor->setTypeMap(['root' => 'array']);

foreach (
$cursor as $document) {
var_dump($document);
}

?>

ไธŠใฎไพ‹ใฎๅ‡บๅŠ›ใฏใ€ ใŸใจใˆใฐไปฅไธ‹ใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚

array(2) {
  ["_id"]=>
  object(MongoDB\BSON\ObjectId)#6 (1) {
    ["oid"]=>
    string(24) "56424fb76118fd3267180741"
  }
  ["x"]=>
  int(1)
}

ๅ‚่€ƒ

๏ผ‹add a note

User Contributed Notes 1 note

up
18
tdrpic ยถ
9 years ago
If you would prefer to have the results returned as an associative array, after executing your query you could call $cursor->setTypeMap like this:

$cursor->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']);
To Top