通过PHP获取ObjectId MongoDB
这是我的剧本通过PHP获取ObjectId MongoDB,php,mongodb,Php,Mongodb,这是我的剧本 $terms = ['username' => $uri]; $user = $collection->findOne($terms); 结果 array (size=4) '_id' => object(MongoId)[29] public '$id' => string '4ff6e96bb0b4599016000006' (length=24) 'username' => string 'me' (length=
$terms = ['username' => $uri];
$user = $collection->findOne($terms);
结果
array (size=4)
'_id' =>
object(MongoId)[29]
public '$id' => string '4ff6e96bb0b4599016000006' (length=24)
'username' => string 'me' (length=10)
'name' => string 'Yes, It's me!' (length=16)
获取名称
$name=$user['name']代码>
但是,如何获取$user[''u id']
我尝试$user[''u id']
不工作
请帮忙。谢谢
更新
用$user['''u id']->{'$id'}
$user['''u id']->{'$id'}
解决的问题不是必需的,也不是难看的,您只需将其转换为字符串即可获得id$id=(string)$user[''u id']代码>您使用的是什么版本的PHP和MongoDB驱动程序?尽管var\u dump输出显示您的\u id是ObjectId,$user['''''
仍应在示例中将其字符串化为公用的“$id”值(4ff6e96bb0b45901600006
)