Database Reference
In-Depth Information
$collection = $c->contacts->people;
// Specify the regular expression
$regex = new MongoRegex("/stradgynl/i");
// Execute the query and store it under the $cursor variable
$cursor = $collection->find(array("Address.Place" => $regex));
//Print the results
while($document = $cursor->getNext())
{
print_r($document);
}
When creating a PHP application, you'll typically want to search for specific data. In the preceding example, you
would probably replace the text ( "stradgynl" , in this case) with a $_POST variable.
Modifying Data with PHP
If we lived in a world where all data remained static and humans never made any typos, we would never need to
update our documents. But the world is a little more flexible than that, and there are times when we make mistakes
that we'd like to correct.
For such situations, you can use a set of modifier functions in MongoDB to update (and therefore change) your
existing data. You can do this in several ways. For example, you might use the update() function to update existing
information, and then use the save() function to save your changes. The following sections look at a handful of these
and other modifier operators, illustrating how to use them effectively.
Updating via update()
As detailed in Chapter 4, you use the update() function to perform most document updates. Like the version
of update() in the MongoDB shell, the update() function that comes with the PHP driver allows you to use an
assortment of modifier operators to update your documents quickly and easily. PHP's version of the update()
function operates almost identically; nevertheless, using the PHP version successfully requires a significantly different
approach. The upcoming section will walk you through how to use the function successfully with PHP.
PHP's update() function takes a minimum of two parameters: the first describes the object(s) to update, and
the second describes the object you want to update the matching record(s) with. Additionally, you can specify a third
parameter for an expanded set of options.
The options parameter provides seven additional flags you can use with the update() function; this list explains
what they are and how to use them:
upsert : If set to true , this Boolean option causes a new document to be created if the search
criteria are not matched.
multiple : If set to true , this Boolean option causes all documents matching the search criteria
to be updated.
fsync : If set to true , this Boolean option causes the data to be synced to disk before returning
a success. If this option is set to true , then it's implied that w is set to 0 , even if it's set
otherwise. Defaults to false.
w : If set to 0, the update operation will not be acknowledged. When working with replica sets,
w can also be set to n , ensuring that the primary server acknowledges the update operation
when successfully replicated to n nodes. Can also be set to 'majority' —a reserved string—to
ensure that the majority of replica nodes will acknowledge the update, or to a specific tag,
ensuring that those nodes tagged will acknowledge the update. This option defaults to 1,
acknowledging the update operation.
 
Search WWH ::




Custom Search