2014-01-13 64 views
0

我想重複使用PHP的MongoDB查詢。MongoDB的PHP查詢只返回子對象項目

以下查詢使用mongo shell工作。

db.user.find({"_id": ObjectId("52d1cedfb1c097b41800055e"), "address.type": "home"},  

{"_id": 0, "address.coordinent.longitude": 1, "address.coordinent.latitude": 1, "address.zipcode": 1}) 

它返回:

{ "address" : [   {  "zipcode" : "84123", "coordinent" : { 
"longitude" : -111.908978,  "latitude" : 40.63147499999999 } } ] } 

我出現以下結果文件:

$db->user->findOne(array("_id" => $mongoID)) 

但使用PHP以下嘗試不起作用:

$db->user->findOne(array("_id" => $mongoID), array("address->type" => "home"},{"_id": 0, "address->coordinent->longitude" => 1, "address->coordinent->latitude" => 1, "address->zipcode" => 1})) 

And:

$db->user->findOne(array("_id" => $mongoID), array("address[type]" => "home"},{"_id": 0, "address[coordinent][longitude]" => 1, "address[coordinent][latitude]" => 1, "address[zipcode]" => 1})) 

在PHP中查詢的正確格式是什麼?

回答

0

您需要在您的mongo shell示例中使用的PHP中使用相同的dot notation

的(未經測試)查詢應該是這個樣子:

<?php 
    $query = array(
     "_id" => $mongoID, 
     "address.type" => "home", 
    ); 

    $projection = array(
     "_id" => 0, 
     "address.coordinent.longitude" => 1, 
     "address.coordinent.latitude" => 1, 
     "address.zipcode" => 1, 
    ); 

    $db->user->findOne($query, $projection); 
?>