doe
doe

Reputation: 73

PHP MongoDB Search in Sub-array

Data stored in collection as below

 [field_1] => Array
                (
                    [fields] => Array
                        (
                            [0] => MongoInt64 Object
                                (
                                    [value] => 1233
                                )

                            [1] => MongoInt64 Object
                                (
                                    [value] => 1234
                                )

                        )

                )

I need to search 1234 in field.

I used below code in php to search

$param = array('field_1.fields.$' => 1234);

But this is not working

Upvotes: 1

Views: 948

Answers (1)

luishdez
luishdez

Reputation: 186

You need to use the $in query criteria, to find elements within an array

$cursor = $collection->find(array("field_1.fields" => array('$in' => array("1234"))));

This will find all items that have 1234 within "fields"

$in doc: https://docs.mongodb.org/v3.0/reference/operator/query/in/

Upvotes: 3

Related Questions