2012-02-08 80 views
0

我對Mongo和PHP相當陌生。我對相對簡單的東西一直很好,但是我遇到了一個在有​​限數量的_id's有限的mongo集合中執行php查找的障礙。

這裏的演練......

// "characters" collection 
{ "_id":{"$id":"3f177b70df1e69fe5c000001"}, "firstname":"Bugs", "lastname":"Bunny" } 
{ "_id":{"$id":"3f2872eb43ca8d4704000002"}, "firstname":"Elmer", "lastname":"Fudd" } 
{ "_id":{"$id":"3f287bb543ca8de106000003"}, "firstname":"Daffy", "lastname":"Duck" } 

// "items" collection 
{ "_id":{"$id":"4f177b70df1e69fe5c000001"}, "mdl":"carrot", "mfg":"Wild Hare Farms ltd.", "ownerid":{"$id":"3f177b70df1e69fe5c000001"} } 
{ "_id":{"$id":"4f2872eb43ca8d4704000002"}, "mdl":"hat", "mfg":"Acme Inc.", "ownerid":{"$id":"3f2872eb43ca8d4704000002"} } 
{ "_id":{"$id":"4f287bb543ca8de106000003"}, "mdl":"spaceship", "mfg":"Acme Inc.", "ownerid":{"$id":"3f287bb543ca8de106000003"} } 

// Let's say I do a find on the item collection for a specific manufacturer... 
$itemOwners = $db->items->find(array("mfg" => "Acme Inc."), array("ownerid")); 

// The result looks something like this... 
[ 
    "4f2872eb43ca8d4704000002":{"_id":{"$id":"4f2872eb43ca8d4704000002"},"ownerid":{"$id":"3f2872eb43ca8d4704000002"}}, 
    "4f287bb543ca8de106000003":{"_id":{"$id":"4f287bb543ca8de106000003"},"ownerid":{"$id":"3f287bb543ca8de106000003"}} 
] 

// I'd now like to perform a find on the character collection and get the corresponding owner documents. 
// To do that I need to build the $in array from the previous find results... 
foreach ($itemOwners as $doc) 
    $itemOwnersTemp[] = $doc["ownerid"]; 
$itemOwners = $itemOwnersTemp; 

// The resulting array looks like this. I've read that the ids need to be in MongoId format. Seems like they are? 
[ 
    {"$id":"3f2872eb43ca8d4704000002"}, 
    {"$id":"3f287bb543ca8de106000003"} 
] 

// and (finally) the conditional find. The result set is always empty. What am I tripping up on? 
$characterDocs = $db->characters->find(array("_id" => array('$in' => $itemOwners)); 

回答

0

我只是略有修改代碼嘗試這樣做:

 
<?php 
$m = new Mongo('localhost:13000', array('replicaSet' => 'a')); 
$db = $m->demo; 
$db->authenticate('derick', 'xxx'); 


// "characters" collection 
$c = $db->characters; 
$c->insert(array('_id' => new MongoID("3f177b70df1e69fe5c000001"), 'firstname' => 'Bugs', 'lastname' => 'Bunny')); 
$c->insert(array('_id' => new MongoID("3f2872eb43ca8d4704000002"), 'firstname' => 'Elmer', 'lastname' => 'Fudd')); 
$c->insert(array('_id' => new MongoID("3f287bb543ca8de106000003"), 'firstname' => 'Daffy', 'lastname' => 'Duck')); 

// "items" collection 
$c = $db->items; 
$c->insert(array('_id' => new MongoId("4f177b70df1e69fe5c000001"), 'mdl' => 'carrot', 'ownerid' => new MongoID('3f177b70df1e69fe5c000001'))); 
$c->insert(array('_id' => new MongoId("4f2872eb43ca8d4704000002"), 'mdl' => 'hat', 'ownerid' => new MongoID('3f2872eb43ca8d4704000002'))); 
$c->insert(array('_id' => new MongoId("4f287bb543ca8de106000003"), 'mdl' => 'space', 'ownerid' => new MongoID('3f287bb543ca8de106000003'))); 

// Let's say I do a find on the item collection for a specific manufacturer... 
$itemOwners = $db->items->find(array("mdl" => "hat"), array("ownerid")); 

// The result looks something like this... 
/*[ 
    "4f2872eb43ca8d4704000002":{"_id":{"$id":"4f2872eb43ca8d4704000002"},"ownerid":{"$id":"3f2872eb43ca8d4704000002"}}, 
    "4f287bb543ca8de106000003":{"_id":{"$id":"4f287bb543ca8de106000003"},"ownerid":{"$id":"3f287bb543ca8de106000003"}} 
]*/ 

// I'd now like to perform a find on the character collection and get the corresponding owner documents. 
// To do that I need to build the $in array from the previous find results... 
foreach ($itemOwners as $doc) { 
    $itemOwnersTemp[] = $doc["ownerid"]; 
} 
$itemOwners = $itemOwnersTemp; 

// The resulting array looks like this. I've read that the ids need to be in MongoId format. Seems like they are? 
/* 
[ 
    {"$id":"3f2872eb43ca8d4704000002"}, 
    {"$id":"3f287bb543ca8de106000003"} 
] 
*/ 

// and (finally) the conditional find. The result set is always empty. What am I tripping up on? 
$characterDocs = $db->characters->find(array("_id" => array('$in' => $itemOwners))); 
var_dump(iterator_to_array($characterDocs)); 
?> 

而且它提供了我所期望的輸出:

 
array(1) { 
    ["3f2872eb43ca8d4704000002"]=> 
    array(3) { 
    ["_id"]=> 
    object(MongoId)#3 (1) { 
     ["$id"]=> 
     string(24) "3f2872eb43ca8d4704000002" 
    } 
    ["firstname"]=> 
    string(5) "Elmer" 
    ["lastname"]=> 
    string(4) "Fudd" 
    } 
} 

地方,我認爲你做了錯誤的轉換,但很難說因爲你不顯示PHP的輸出。

+0

謝謝Derick ...你的代碼幫助我找到我的問題。這不是MongoId或PHP。在最後發現的嘆息中,這是一個集合名稱中的微不足道的錯字。 – 2012-02-08 18:25:33

0

取而代之的是:

$itemOwnersTemp[] = $doc["ownerid"]; 

試試這個:

$itemOwnersTemp[] = new MongoID($doc["ownerid"]); 
+0

我在想同樣的事情,但它似乎沒有任何區別... – 2012-02-08 03:04:49