MadeOfSport
MadeOfSport

Reputation: 513

mongodb - search in subarray, PHP

i started with mongodb and played around with random temperature data like this:

'weather' => array(
                    'Air' => array(
                        'Jan' => 11,
                        'Feb' => 20,
                        'Mar' => 24,
                        'Jun' => 28,
                        'Jul' => 30
                    )
                ),

Now my question: How can i query the Air array ?

I knwo i can do somethin like:

$query = array('weather.Air.Jan' => 11);

Works fine...

But how can i search in the whole Air array:

$query = array('weather.Air.$' => 40);

This query doesn't work... Can somebody help me

Upvotes: 2

Views: 2122

Answers (1)

Gates VP
Gates VP

Reputation: 45307

Unfortunately, the query you're looking for does not exist.

As written, you're asking for "weather.Air where a key in the JSON object contains a value of 40".

MongoDB has the ability to "drill into" arrays. However, when it comes to sub-objects, you have to reach into the keys directly. There is no operator that provides a "search all keys" method. There is an outstanding JIRA request for this item right here.

Upvotes: 2

Related Questions