Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
should I do it before registering to sessin or just before querying? – mustafa Apr 23 at 19:57
before querying – Howard Apr 23 at 19:58
I think still returns empty – mustafa Apr 23 at 20:01
Try var_dumping the entire array being sent to your query. That is, the _id and $in included. – Howard Apr 23 at 20:04
seems there is just one id passing to query var_dump($selection); object(MongoId)#19 (1) { ["$id"]=> string(24) "5176d1f99de5ee2808030b59" } – mustafa Apr 23 at 20:09
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.