mongo php find $in 与 _id 的问题

发布于 2025-01-03 11:09:34 字数 1960 浏览 1 评论 0原文

我对 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));

I'm fairly new to Mongo and PHP. I've been fine with the relatively simple stuff but I've hit a snag performing a php find within a mongo collection conditionally limited by an array of _id's.

Here's a walkthrough...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你是孤单的心事 2025-01-10 11:09:34

我刚刚用稍微修改过的代码尝试过这个:

<?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 的输出。

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.

苯莒 2025-01-10 11:09:34

而不是这个:

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

试试这个:

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

Instead of this:

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

Try this:

$itemOwnersTemp[] = new MongoID($doc["ownerid"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文