How do you get the string value of a MongoID using PHP? How do you get the string value of a MongoID using PHP? mongodb mongodb

How do you get the string value of a MongoID using PHP?


Believe this is what you're after.

$widget['_id']->{'$id'};

Something like this.

$widget = array('text' => 'Some text');$this->mongo->db->insert($widget);$widget['widgetId'] = $widget['_id']->{'$id'};echo json_encode($widget);


You can also use:

(string)$widget['_id']


correct way is use ObjectId from MongoDB:

function getMongodbIDString($objectId){    $objectId = new \MongoDB\BSON\ObjectId($objectId);    return $objectId->jsonSerialize()['$oid'];}

and do not cast the objectId like (string) $row['_id'] or $row->_id->{'$oid'}