1

I'm new to mongo and I'm getting an error. Both $user_id and $this->_id are non empty (valid mongo id).

$criteria = array( 
    '$and' => array( 
        array('parent_id' =>$this->_id), 
        '$or' => array(
            array('user_id' => $user_id),
            array('from_user_id' => $user_id)
        )               
    ) 
);

the error says

$and expression must be a nonempty array 

Any clue?

1 Answer 1

3

You have not defined your array properly:

$criteria = array(
  '$and' => array(
    array('parent_id' => $this->_id ),
    array('$or' => array(
      array('user_id' => $user_id ),
      array('from_user_id' => $user_id ) 
    ))

) );

Try to check your structures before submitting as a query to make sure they format as you expect:

echo json_encode( $criteria, JSON_PRETTY_PRINT ) ."\n";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this answer. I know now how to validate the structure of my query by using json_encode with JSON_PRETTY_PRINT

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.