mongo php发现_id的问题

时间:2012-02-08 02:36:02

标签: php mongodb find conditional

我是Mongo和PHP的新手。我对相对简单的东西一直很好,但是我遇到了一个在一个mongo集合中执行php查找的障碍,该集合有条件地受到_id数组的限制。

这是演练......

// "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));

2 个答案:

答案 0 :(得分:0)

而不是:

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

试试这个:

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

答案 1 :(得分: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的输出。