Som*_*gOn 10 mongodb mongodb-php
执行插入后,我想使用json_encode()将对象传递给客户端.问题是,_id值不包括在内.
$widget = array('text' => 'Some text');
$this->mongo->db->insert($widget);
If I echo $widget['_id'] the string value gets displays on the screen, but I want to do something like this:
$widget['widgetId'] = $widget['_id']->id;
So I can do json_encode() and include the widget id:
echo json_encode($widget);
Run Code Online (Sandbox Code Playgroud)
Joh*_*ast 43
相信这就是你所追求的.
$widget['_id']->{'$id'};
Run Code Online (Sandbox Code Playgroud)
像这样的东西.
$widget = array('text' => 'Some text');
$this->mongo->db->insert($widget);
$widget['widgetId'] = $widget['_id']->{'$id'};
echo json_encode($widget);
Run Code Online (Sandbox Code Playgroud)
正确的方法是使用 MongoDB 中的 ObjectId:
function getMongodbIDString($objectId){
$objectId = new \MongoDB\BSON\ObjectId($objectId);
return $objectId->jsonSerialize()['$oid'];
}
Run Code Online (Sandbox Code Playgroud)
并且不要像(string) $row['_id']或$row->_id->{'$oid'}
| 归档时间: |
|
| 查看次数: |
19840 次 |
| 最近记录: |