html
php
css
ajax
python
mysql
database
linux
android
ruby-on-rails
regex
objective-c
eclipse
flash
oracle
tsql
apache
asp
api
dom
I've just tried this with slightly modified code:
<?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 ) ); ?>
And it provides the output that I expect:
array(1) { ["3f2872eb43ca8d4704000002"]=> array(3) { ["_id"]=> object(MongoId)#3 (1) { ["$id"]=> string(24) "3f2872eb43ca8d4704000002" } ["firstname"]=> string(5) "Elmer" ["lastname"]=> string(4) "Fudd" } }
Somewhere, I think you're doing a wrong conversion but it's difficult to tell as you don't show the output of PHP.
Instead of this:
$itemOwnersTemp[] = $doc["ownerid"];
Try this:
$itemOwnersTemp[] = new MongoID($doc["ownerid"]);