0

i am working on mongodb & php & want to retrive data based on multiple conditions.

I want to retrive forms whose form_status is both Active & Draft only

My query is:

$formData = $formInfo->find(array('team_id' => $_GET['id'], '$and' =>array('form_status' => 'Active','form_status' => 'Draft')));

It is not working. What could be the right syntax in PHP??

1 Answer 1

1

The $and operator takes a "real array" of documents as it's argument. In PHP you wrap the array to produce that kind of syntax:

$formData = $formInfo->find(
    array(
        'team_id' => $_GET['id'], 
        '$and' => array(
            array( 'form_status' => 'Active' ),
            array( 'form_status' => 'Draft' )
        )
    )
);

Note that this really woudn't make any sense unless "form_status" is actually and array itself. In which case the $all operator is a much cleaner approach:

$formData = $formInfo->find(
    array(
        'team_id' => $_GET['id'], 
        'form_status' => array( 
            '$all' => array( 'Active', 'Draft' )
        )
    )
);

And again if this field was not an array then you really meant $or but that can also be more clearly written for the same field with $in:

$formData = $formInfo->find(
    array(
        'team_id' => $_GET['id'], 
        'form_status' => array( 
            '$in' => array( 'Active', 'Draft' )
        )
    )
);

So $all is to $and what $in is to $or, but just allows you to use the same field without specifying the full document form

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

4 Comments

$formData = $formInfo->find(array('team_id' => $_GET['id'],'$and' => array(array( 'form_status' => 'Active' ),array( 'form_status' => 'Draft' )))); foreach($formData as $data){ var_dump($data); } die;
@Shaggie I think you have your terms incorrect. I was actually just editing that to explain that point. See the additions.'
ok. when i was commenting at that time recent edit was not there
thanks your last post for query with $in works perfect...thanks a lot

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.