I really have a problem to understand how to build nested and/or statements in a PHP MongoDB query-array. For simple understanding, this is what I would write in SQL:
SELECT * FROM message where (fromid = '$myfromid' AND toid = '$mytoid') OR (fromid = '$mytoid' AND toid = '$myfromid')
I have a MongoCollection already set to the message-collection and build a cursor with this condition-array:
$arr_Search = array(
'$or' => array(
array( '$and' => array(
array('fromid' => $myfromid, 'toid' => $mytoid )
) ),
array( '$and' => array(
array('fromid' => $mytoid, 'toid' => $myfromid )
) )
)
);
It works, but not as expected. It gives me all datasets where only "fromid" is $myfromid. I have tried it with this:
$arr_Search = array(
'$or' => array(
array('fromid' => $myfromid, 'toid' => $mytoid ),
array('fromid' => $mytoid, 'toid' => $myfromid )
)
);
But the same here. I have searched a lot about nested statements, but I think I have a big misunderstand in this. Can anyone help me with this example?
Thanks a lot! Sebastian
Hmm. Don't know what I made earlier, but it seems to work now. For all searching about this, this is the right way to handle it:
$arr_Search = array(
'$or' => array(
array('fromid' => $fromid, 'toid' => $toid ),
array('fromid' => $toid, 'toid' => $fromid )
)
);
Thanks, Sebastian