我发现当我尝试填充一个关联数组(mongoguid => stringValue)时,我不得不手动调用 __toString 方法。
$filter = [];
$options = [];
$this->propertyMap = [];
try {
$query = new \MongoDB\Driver\Query($filter, $options);
$cursor = $this->mongo->executeQuery('MyDbName.MyColectionName', $query);
} catch (Exception $e) {
echo "Mongo 查询失败,无法提取集合" . PHP_EOL;
echo($e->getMessage());
die("aborting");
}
foreach ($cursor as $property) {
//$this->propertyMap[$property->{_id}->__toString()] = $property->name;
$this->propertyMap[$property->name] = $property->{_id}->__toString();
}
你也可以使用 $property->{_id} . "" 来获得相同的字符串结果。否则,你会得到一个包含 MongoDB\BSON\ObjectID 对象的数组。