Andrew
Andrew

Reputation: 421

Mongo DB $or query in PHP

I can't figure out for the life of my to select from a collection with the or parameter. It's not working at all for me and I can't really find any documentation on it for php.

Here is my example code that doesn't return anything even though they exist in the collection:

$cursor = $products->find(
    array(
        '$or' => array(
            "brand" => "anti-clothes",
            "allSizes" => "small"
        )
    )
);

Upvotes: 17

Views: 34595

Answers (1)

Karoly Horvath
Karoly Horvath

Reputation: 96266

The $or operator lets you use boolean or in a query.
You give $or an array of expressions, any of which can satisfy the query.

You provided only one element in the array. Use:

find(array('$or' => array(
  array("brand" => "anti-clothes"),
  array("allSizes" => "small")
)));

Upvotes: 52

Related Questions