如何使用'php'获取街道价值?
{
_id : "001",
name : "fakename",
address : {
street : "12 Street",
city : "Cosmos",
}
}
查询:
$collection = database_name->collection_name;
$cursor = $collection->find ( array('name' => 'fakename' ), array( 'address.street' ) );
foreach ( $cursor as $doc ) {
echo $doc[ 'address.street' ];
}
结果:
$doc[ 'address.street' ]
值无法打印。
答案 0 :(得分:0)
PHP使用多维数组:
$doc['address']['street']
答案 1 :(得分:0)
$db = (new \MongoDB\Client("mongodb://localhost:27017"))->database_name;
$collection = $db->collection_name;
$cursor = $collection->find ( array('name' => 'fakename' ), array( 'address.street' ) );
foreach ($cursor as $doc) {
echo $doc['address']['street'];
}