2016-12-05 100 views
0

如何使用'php'獲取街道價值?使用php從mongoDB獲得價值

{ 

    _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
$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']; 
} 
+0

已解決!感謝您的答案。 – YSuraj

0

PHP使用多維數組:

$doc['address']['street'] 
+0

感謝。是的,它使用多維數組。 – YSuraj

+0

@YSuraj如果這已回答您的問題,請不要忘記將答案標記爲正確,以便您的問題得到解決。 –