Монго php найти $ в проблеме с _id's - PullRequest
0 голосов
/ 08 февраля 2012

Я довольно новичок в Mongo и PHP.Я был в порядке с относительно простыми вещами, но я наткнулся на загадку, выполняя поиск 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 голосов
/ 08 февраля 2012

Я только что попробовал это с немного измененным кодом:

<?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 голосов
/ 08 февраля 2012

Вместо этого:

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

Попробуйте это:

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