downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

MongoCollection::validate> <MongoCollection::__toString
[edit] Last updated: Fri, 18 Sep 2009

view this page in

MongoCollection::update

(PECL mongo >=0.9.0)

MongoCollection::updateUpdate records based on a given criteria

Описание

public boolean MongoCollection::update ( array $criteria , array $newobj [, boolean $upsert = FALSE ] )

Параметри

criteria

Description of the objects to update.

newobj

The object with which to update the matching records.

upsert

If $newobj should be inserted if the criteria is not found.

Връщани стойности

Returns if the update was successfully sent to the database.



MongoCollection::validate> <MongoCollection::__toString
[edit] Last updated: Fri, 18 Sep 2009
 
add a note add a note User Contributed Notes MongoCollection::update
nerds at limeworks dot com dot au 22-Aug-2011 02:39
For anyone referencing records by the Mongo _id object, it's important to recognise that it is in fact an object, and not a string.

If you have a record with a Mongo ID of say "4e519d5118617e88f27ea8cd" that you are trying to retrieve or update, you cannot search for it using something like:
<?php
$m
= new Mongo();
$db = $m->selectDB('db');
$collection = 'collection';
$db->$collection->findOne(array('_id', '4e519d5118617e88f27ea8cd'));
?>

There is some documentation that mentions simple conversion to string will solve this, but I have found the only reliable way to locate records based on their ID is to first pass it to MondoID(), then use that for reference.

Something like this will be far more reliable:
<?php
$m
= new Mongo();
$db = $m->selectDB('db');
$collection = 'collection';
$mongoID = new MongoID('4e519d5118617e88f27ea8cd');
$db->$collection->findOne(array('_id', $mongoID));
?>

This may prove useful for anyone using the ID object like an auto-increment database key would be used in MySQL or similar.
joshuadburns at hotmail dot com 08-Nov-2010 12:51
Please note under optional third parameter "options":

While the official MongoDB documentation references the keyword "multi" to flag the use of multiple updates, the PHP implementation uses the key "multiple" instead.

This may cause a little confusion if you're basing your keys on the OFFICIAL MongoDB documentation.
rithish[at]gmail[dot]com 02-Nov-2010 05:22
Do note, for incrementing a value using $inc, typecast the value to an integer before passing the new object to update().

<?php
$votes
= (int) $votes;
$newData = array('$inc' => array('votes'=>$votes));
$c->update(array("firstname" => "Bob"), $newData);
?>

This is especially noteworthy, if you are taking values from $_GET and pushing them for increment.
jeff at canuckistani dot ca 17-Sep-2010 03:05
The return type of update changed in 1.09 if you are using safe => TRUE. It now returns something that looks like the info returned by MongoDB::lastError:

Array
(
    [err] =>
    [updatedExisting] => 1
    [n] => 1
    [ok] => 1
)

 
show source | credits | stats | sitemap | contact | advertising | mirror sites