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.
MongoCollection::update
(PECL mongo >=0.9.0)
MongoCollection::update — Update 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.
nerds at limeworks dot com dot au
22-Aug-2011 02:39
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
)
