Database Reference
In-Depth Information
// Specify the search criteria
$criteria = array("Family Name" => "Wood");
// Specify the information to be added (partially successful; some
// examples were already there)
$update = array(
'$addToSet' => array
(
"E-Mail" => array
(
'$each' => array
(
"vw@mongo.db",
"vicky@mongo.db",
" vicky@example.com "
)
)
)
);
// Perform the update
$collection->update($criteria,$update);
Removing an Element from an Array with $pop
PHP's $pop operator lets you remove an element from an array. Keep in mind that you can remove only the first or last
element in the array—and nothing in between. You remove the first element by specifying a value of -1 ; similarly,
you remove the last element by specifying a value of 1 :
// 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");
// Pop out the first e-mail address found in the list
$update = array('$pop' => array("E-Mail" => -1));
// Perform the update
$collection->update($criteria,$update);
Specifying a value of -2 or 1000 wouldn't change which element is removed. any negative number will remove
the first element, whereas any positive number removes the last element. Using a value of 0 removes the last element
from the array.
Note
 
 
Search WWH ::




Custom Search