1

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.