我发现当我尝试填充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("终止");
}
foreach ($cursor as $property) {
//$this->propertyMap[$property->{_id}->__toString()] = $property->name;
$this->propertyMap[$property->name] = $property->{_id}->__toString();
}
你也可以使用 $property->{_id} . "" 来获得相同的字符串结果。否则你最终会得到一个MongoDB\BSON\ObjectID对象的数组。