Database Reference
In-Depth Information
Matching All Criteria in a Query with $all
Like the $in operator, the $all operator lets you compare against multiple values in an additional array. The
difference is that the $all operator requires that all items in the array match a document before it returns any results.
The following example shows how to conduct such a query:
// Connect to the database
$c = new MongoClient();
// Select the collection 'people' from the database 'contacts'
$collection = $c->contacts->people;
// Specify the conditional operator
$cond = array('E-Mail' => array('$all' => array(" vw@example.com " ," vw@office.com " )));
// Execute the query and store it under the $cursor variable
$cursor = $collection->find($cond);
//Print the results
while($document = $cursor->getNext())
{
print_r($document);
}
Searching for Multiple Expressions with $or
You can use the $or operator to specify multiple expressions a document can contain to return a match. The
difference between the two operators is that the $in operator doesn't allow you to specify both a key and value,
whereas the $or operator does. You can combine the $or operator with any other key/value combination. Let's look
at two examples.
The first example searches for and returns any document that contains either an Age key with the integer value of
28 or an Address.Country key with the value of USA :
// Connect to the database
$c = new MongoClient();
// Select the collection 'people' from the database 'contacts'
$collection = $c->contacts->people;
// Specify the conditional operator
$cond = array('$or' => array(
array("Age" => 28),
array("Address.Country" => "USA")
) );
// Execute the query and store it under the $cursor variable
$cursor = $collection->find($cond);
//Print the results
while($document = $cursor->getNext())
{
print_r($document);
}
 
Search WWH ::




Custom Search