无法在 php 中获取 mongodb _id 值

Unable to get mongodb _id value in php

为了得到mongodb_id的值,我尝试了很多技巧,但都出错了。我尝试了这些:

$new_id = new MongoId();
print_r($new_id['_id']); // -> Internal Server Error
echo $new_id['_id']->{'$id'}; // -> Internal Server Error
print_r($new_id); // no error, but useless

所以,我想知道如何正确地做到这一点

这段代码对我有用:

$mongoId = new \MongoId();
$strId = $mongoId->__toString();

print_r($strId); die;

MongoId::__toString — Returns a hexidecimal representation of this id

public string MongoId::__toString ( void )

http://php.net/manual/en/mongoid.tostring.php

否则请检查您的服务器错误日志。