Database Reference
In-Depth Information
// Specify the search criteria
$criteria = array("Last Name" => "Wood");
// Specify the information to be removed
$update = array('$unset' => array("Phone" => 1));
// Perform the update
$collection->update($criteria,$update);
Renaming a Field with $rename
The $rename operator can be used to rename a field. This can be helpful when you've accidently made a typo or
simply wish to change its name to a more accurate one. The operator will search for the given field name within each
document and its underlying arrays and subdocuments.
Be careful when using this operator. If the document already contains a field that has the new name,
that field will be deleted, after which the old fieldname will be renamed to the new one as specified.
Warning
Let's look at an example where the First Name and Last Name fields will be renamed to Given Name and Family
Name , respectively, for Vicky Wood:
// Connect to the database
$c = new MongoClient();
// Select the collection 'people' from the database 'contacts'
$collection = $c->contacts->people;
// Specify the search criteria
$criteria = array("Last Name" => "Wood");
// Specify the information to be changed
$update = array('$rename' => array("First Name" => "Given Name", "Last Name" => "Family Name"));
// Perform the update
$collection->update($criteria,$update);
Changing the Value of a Key during Upsert with $setOnInsert
PHP's $setOnInsert operator can be used to assign a specific value only in case the update function performs an
insert when using the upsert operator. This might sound a bit confusing at first, but you can think of this operator as
a conditional statement that only sets the given value when upsert inserts a document, rather than updates one. Let's
look at an example to clarify how this work. First, we'll perform an upsert that matches an existing document, thus
ignoring the $setOnInsert criteria specified:
// Connect to the database
$c = new MongoClient();
// Select the collection 'people' from the database 'contacts'
$collection = $c->contacts->people;
// Specify the search criteria
$criteria = array("Family Name" => "Wood");
// Specify the information to be set on upsert-inserts only
$update = array('$setOnInsert' => array("Country" => "Unknown"));
 
 
Search WWH ::




Custom Search