使用php从mongoDB获取价值

时间:2016-12-05 11:26:59

标签: php mongodb

如何使用'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' ]

值无法打印。

2 个答案:

答案 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'];
}