Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb php query, search in array?

I am looking for a way to search for a keyword (ex. "Henrik") in the "answers" array of documents with the following structure

Array
(
[_id] => MongoId Object
    (
        [$id] => 4eeedd9545c717620a000007
    )

[formId] => 6
[respId] => 4eeedd95c93228
[dateCreated] => 2011-10-14 07:45
[answers] => Array
    (
        [field1] => Henrik
        [field6] => [email protected]
        [field7] => my city address
    )

)

I am working in PHP on this project, and quering like this works of course:

$answers = $collection->find( array('formId' => 6, 'answers.field1'=> 'Henrik' ) );

What I want to do is search without a specific key of the answers array, like this

$answers = $collection->find( array('formId' => 6, 'answers'=> 'Henrik' ) );

Is it possible to do this type of query? I am sorry if this is a repost. I was not able to find any examples about this here or on Google.

like image 548
Henkealg Avatar asked Dec 05 '22 18:12

Henkealg


1 Answers

$answers = $collection->find( array('formId' => 6, 
           'answers'=>
                      array('$in' => array('Henrik'))));
like image 83
vyc Avatar answered Dec 10 '22 11:12

vyc