MongoDB\Driver\Cursor::setTypeMap

(mongodb >=1.0.0)

MongoDB\Driver\Cursor::setTypeMapSets a type map to use for BSON unserialization

Description

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

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

Parameters

typeMap (array)

Type map configuration.

Return Values

No value is returned.

Errors/Exceptions

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

Examples

Example #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);
}

?>

The above example will output something similar to:

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

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial