mongo中的_id是一個objectid對象類型,不管是查詢時作為條件,還是列表時展示內容,都需要進行一下抓換
查詢時要轉為objectid對象
列表時要把對象轉成字符串覆蓋回_id字段
$filter['_id']=new MongoDB\BSON\ObjectId($params['id']);; $options=[]; $collect='.modelHis'; $mongoManger = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017"); $query = new MongoDB\Driver\Query($filter, $options); $cursor = $mongoManger->executeQuery($dbName.$collect, $query); if($cursor->isDead()){ //return []; } $items=[]; foreach ($cursor as $document) { $document->_id=$document->_id->__toString(); $items[]=get_object_vars($document); }