MongoDB 和 PHPLib 从游标中检索嵌入文档

MongoDB and PHPLib retrieving an embedded document from a cursor

我有以下代码:

<?php

require __DIR__ . '/vendor/autoload.php';

$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->bbcData->programmeData;
$result = $collection->find( [ '_id' => 'b007jwd9'] );

foreach ($result as $entry) {
    echo $entry['_id'], ': ', $entry['complete_title'], "\n";
}

?>

JSON如下:

{"complete_title":{"name":"wimsey - murder must advertise","episode":"sudden decease of a man in dress clothes"},"media_type":"audio","_id":"b007jwd9","categories":{"category1":["drama", "crime"]}}

我想打印出 complete_title 的名称,但是文档没有显示这个的语法。目前我收到一条错误消息,指出 complete_title 是一个 BSONDocument,因此无法将其转换为字符串。

使用iterator_to_array获取数组。

foreach ($result as $entry) {
    $array = iterator_to_array($entry['complete_title']);
    echo $entry['_id'], ': ', $array['name'], "\n";
}