我發現,當我嘗試填充一個 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 物件的陣列。