我们如何查询对象ids的mongodb数组?(How can we query mongodb array of Objects ids?)

我有一个由对象id组成的子数组

"selections" : ["5176d1f09de5ee2808028da9", "5176d1f09de5ee2808028e4d", "5176d1f09de5ee28080292fe", "5176d1f19de5ee2808029867"]

当用户登录时,这些是使用PHP会话注册的:

var_dump($selections); array(16) { [0]=> string(24) "5176d1f09de5ee2808028a7c" [1]=> string(24) "5176d1f09de5ee2808029180" [2]=> string(24) "5176d1f09de5ee2808029283" [3]=> string(24) "5176d1f19de5ee280802990c"}

我从会话中获取它们,然后在查询中使用它们:

$selectionsFromSession= $_SESSION['selections']; $list=$collection->find(array("_id"=>array('$in'=> $selectionsFromSession )), ....

此查询不返回任何内容 这可能是什么问题?

I have a sub array which consists of object ids

"selections" : ["5176d1f09de5ee2808028da9", "5176d1f09de5ee2808028e4d", "5176d1f09de5ee28080292fe", "5176d1f19de5ee2808029867"]

When users log in, these are registered using PHP sessions:

var_dump($selections); array(16) { [0]=> string(24) "5176d1f09de5ee2808028a7c" [1]=> string(24) "5176d1f09de5ee2808029180" [2]=> string(24) "5176d1f09de5ee2808029283" [3]=> string(24) "5176d1f19de5ee280802990c"}

I get them from sessions and then use them in a query:

$selectionsFromSession= $_SESSION['selections']; $list=$collection->find(array("_id"=>array('$in'=> $selectionsFromSession )), ....

This query returns nothing. What can be the problem here?

最满意答案

您需要实例化MongoId对象。

foreach( $selections as &$selection ) { $selection = new \MongoId( $selection ); }

然后,将实例化对象数组传递给查询。

You need to instantiate the MongoId Object.

foreach( $selections as &$selection ) { $selection = new \MongoId( $selection ); }

You then pass the array of instantiated objects to your query.

更多推荐