MongoDB\Driver\Cursor::setTypeMap
PHP function
Edit on GitHub ✎
(mongodb >=1.0.0)
Sets a type map to use for BSON unserialization
Description
MongoDB\Driver\Cursor::setTypeMap(array $typemap): void
Sets the type map configuration to use when unserializing the BSON results into PHP values.
Parameters
Return Values
Void
Errors/Exceptions
When iterating over the cursor, the following exceptions can also be thrown due to an incorrect type map configuration:
- Throws
MongoDB\Driver\Exception\InvalidArgumentExceptionif a class in the type map cannot be instantiated or does not implementMongoDB\BSON\Unserializable.
Examples
MongoDB\Driver\Cursor::setTypeMap() example
php
<?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);
}
?>The above example will output something similar to:
output
array(2) {
["_id"]=>
object(MongoDB\BSON\ObjectId)#6 (1) {
["oid"]=>
string(24) "56424fb76118fd3267180741"
}
["x"]=>
int(1)
}