Database Reference
In-Depth Information
// Execute the query and store it under the $cursor variable
$cursor = $collection->find($cond);
//Print the results
while($document = $cursor->getNext())
{
print_r($document);
}
Finding Documents that Don't Match a Value
You can use the $ne (not equals) operator to find any documents that don't match the value specified in the $ne
operator. The syntax for this operator is straightforward. The next example will display any contact whose age is not
equal to 28:
// 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('Age' => array('$ne' => 28));
// Execute the query and store it under the $cursor variable
$cursor = $collection->find($cond);
//Print the results
while($document = $cursor->getNext())
{
print_r($document);
}
Matching Any of Multiple Values with $in
The $in operator lets you search for documents that match any of several possible values added to an array, as in the
following example:
// 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('Address.Country' => array('$in' => array("USA","UK")));
// Execute the query and store it under the $cursor variable
$cursor = $collection->find($cond);
//Print the results
while($document = $cursor->getNext())
{
print_r($document);
}
The resulting output would show any contact information from any person you add, whether that person lives in
the US or the UK. Note that the list of possibilities is actually added in an array; it cannot be typed in “just like that.”
 
Search WWH ::




Custom Search