Database Reference
In-Depth Information
// Specify the conditional operator
$cond = array('Age' => array('$exists' => false));
// Execute the query and store it under the $cursor variable
$cursor = $collection->find($cond);
//Print the results
while($document = $cursor->getNext())
{
print_r($document);
}
Similarly, the next example returns any contacts that have the Street field set:
// 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.Street" => array('$exists' => true));
// Execute the query and store it under the $cursor variable
$cursor = $collection->find($cond);
//Print the results
while($document = $cursor->getNext())
{
print_r($document);
}
Regular Expressions
Regular expressions are neat. You can use them for just about everything (except for making coffee, perhaps); and they
can greatly simplify your life when searching for data. The PHP driver comes with its own class for regular expressions:
the MongoRegex class. You can use this class to create regular expressions, and then use them to find data.
The MongoRegex class knows six regular expression flags that you can use to query your data. You may already be
familiar with some of them:
i : Triggers case insensitivity.
m : Searches for content that is spread over multiple lines (line breaks).
x : Allows your search to contain #comments.
l : Specifies a locale.
s : Also known as dotall , "." can be specified to match everything, including new lines.
u : Matches Unicode.
Now let's take a closer look at how to use regular expressions in PHP to search for data in your collection.
Obviously, this is best demonstrated with a simple example.
For example, assume you want to search for a contact about whom you have very little information. For example,
you may vaguely recall the place where the person lives and that it contains something like s tradgynl in the middle
somewhere. Regular expressions give you a simple yet elegant way to search for such a person:
// Connect to the database
$c = new MongoClient();
// Select the collection 'people' from the database 'contacts'
 
Search WWH ::




Custom Search