I loop through the documents of my collection, do some stuff, and then update the database. But as I actually have all the data of the document I’m updating, would save() be faster than of update() if I do it that way?
foreach ($cursor as $doc) {
$doc['new_field'] = 'value';
$coll->save($doc);
/* or (currently) */
$coll->update(array('known_field' => $doc['known_field']), array('$set' => array('new_field' => 'value')));
}
Which way is faster?
::updateshould be faster because it update only some fields of a document.::savesave entire document and probably will slower.In general better to use
::updatewhere it possible, because if you using::savepossible concurrency problems. For example if two threads has loaded same document, update it and then trying to save. Lets say first thread has saved document. Then second thread rewrite changes of first thread and you lose updates of first thread. With atomic update you never get stuck on this problem.