<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Update records based on a given criteria</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocollection.--tostring.html">MongoCollection::__toString</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocollection.validate.html">MongoCollection::validate</a></div> <div class="up"><a href="class.mongocollection.html">MongoCollection</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongocollection.update" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoCollection::update</h1> <p class="verinfo">(PECL mongo >=0.9.0)</p><p class="refpurpose"><span class="refname">MongoCollection::update</span> — <span class="dc-title">Update records based on a given criteria</span></p> </div> <div class="refsect1 description" id="refsect1-mongocollection.update-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><span class="type bool|array">bool|array</span></span> <span class="methodname"><strong>MongoCollection::update</strong></span> ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$criteria</code></span> , <span class="methodparam"><span class="type">array</span> <code class="parameter">$new_object</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code><span class="initializer"> = array()</span></span> ] )</div> </div> <div class="refsect1 parameters" id="refsect1-mongocollection.update-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"> <em><code class="parameter">criteria</code></em> </span> <dd> <p class="para"> Description of the objects to update. </p> </dd> </dt> <dt> <span class="term"> <em><code class="parameter">new_object</code></em> </span> <dd> <p class="para"> The object with which to update the matching records. </p> </dd> </dt> <dt> <span class="term"> <em><code class="parameter">options</code></em> </span> <dd> <p class="para"> This parameter is an associative array of the form <em>array("optionname" => <boolean>, ...)</em>. Currently supported options are: <ul class="itemizedlist"> <li class="listitem"> <p class="para"> <em>"upsert"</em> </p> <p class="para"> If no document matches <em><code class="parameter">$criteria</code></em>, a new document will be inserted. </p> <p class="para"> If a new document would be inserted and <em><code class="parameter">$new_object</code></em> contains atomic modifiers (i.e. <em>$</em> operators), those operations will be applied to the <em><code class="parameter">$criteria</code></em> parameter to create the new document. If <em><code class="parameter">$new_object</code></em> does not contain atomic modifiers, it will be used as-is for the inserted document. See the upsert examples below for more information. </p> </li> <li class="listitem"> <p class="para"> <em>"multiple"</em> </p> <p class="para"> All documents matching $criteria will be updated. <span class="function"><strong>MongoCollection::update()</strong></span> has exactly the opposite behavior of <span class="function"><a href="mongocollection.remove.html" class="function">MongoCollection::remove()</a></span>: it updates one document by default, not all matching documents. <em class="emphasis">It is recommended that you always specify whether you want to update multiple documents or a single document</em>, as the database may change its default behavior at some point in the future. </p> </li> <li class="listitem"><p class="para"><em>"fsync"</em></p><p class="para">Boolean, defaults to <strong><code>FALSE</code></strong>. If journalling is enabled, it works exactly like <em>"j"</em>. If journalling is not enabled, it forces the insert to be synced to disk before returning success. If <strong><code>TRUE</code></strong>, an acknowledged insert is implied and will override setting <em>w</em> to <em>0</em>.</p><blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">This option is <em class="emphasis">deprecated</em>. Please use the <em>"j"</em> option instead.</p></p></blockquote></li> <li class="listitem"><p class="para"><em>"j"</em></p><p class="para">Boolean, defaults to <strong><code>FALSE</code></strong>. Forces the insert to be synced to the journal before returning success. If <strong><code>TRUE</code></strong>, an acknowledged insert is implied and will override setting <em>w</em> to <em>0</em>.</p></li> <li class="listitem"><p class="para"><em>"w"</em></p><p class="para">See <a href="mongo.writeconcerns.html" class="link">WriteConcerns</a>. The default value for <a href="class.mongoclient.html" class="classname">MongoClient</a> is <em>1</em>.</p></li> <li class="listitem"><p class="para"><em>"wtimeout"</em></p><p class="para">How long to wait for <a href="mongo.writeconcerns.html" class="link">WriteConcern</a> acknowledgement. The default value for <a href="class.mongoclient.html" class="classname">MongoClient</a> is <em>10000</em> milliseconds.</p></li> <li class="listitem"><p class="para"><em>"safe"</em></p><p class="para"><em class="emphasis">Deprecated</em>. Please use the <a href="mongo.writeconcerns.html" class="link">WriteConcern</a> <em>w</em> option.</p></li> <li class="listitem"><p class="para"><em>"timeout"</em></p><p class="para">Integer, defaults to <em>MongoCursor::$timeout</em>. If acknowledged writes are used, this sets how long (in milliseconds) for the client to wait for a database response. If the database does not respond within the timeout period, a <a href="class.mongocursortimeoutexception.html" class="classname">MongoCursorTimeoutException</a> will be thrown.</p></li> </ul> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-mongocollection.update-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an array containing the status of the update if the <em>"w"</em> option is set. Otherwise, returns <strong><code>TRUE</code></strong>. </p> <p class="para"> Fields in the status array are described in the documentation for <span class="function"><a href="mongocollection.insert.html" class="function">MongoCollection::insert()</a></span>. </p> </div> <div class="refsect1 errors" id="refsect1-mongocollection.update-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para">Throws <a href="class.mongocursorexception.html" class="classname">MongoCursorException</a> if the "w" option is set and the write fails.</p><p class="para">Throws <a href="class.mongocursortimeoutexception.html" class="classname">MongoCursorTimeoutException</a> if the "w" option is set to a value greater than one and the operation takes longer than <var class="varname"><var class="varname">MongoCursor::$timeout</var></var> milliseconds to complete. This does not kill the operation on the server, it is a client-side timeout. The operation in <em>MongoCollection::$wtimeout</em> is milliseconds.</p> </div> <div class="refsect1 changelog" id="refsect1-mongocollection.update-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>1.3.0</td> <td> The <em><code class="parameter">options</code></em> parameter no longer accepts a boolean to signify an upsert. Instead, this now has to be done with <em>array('upsert' => true)</em>. </td> </tr> <tr> <td>1.2.11</td> <td> Emits <strong><code>E_DEPRECATED</code></strong> when <em><code class="parameter">options</code></em> is <span class="type"><span class="type scalar">scalar</span></span>. </td> </tr> <tr> <td>1.2.0</td> <td>Added <em>"timeout"</em> option.</td> </tr> <tr> <td>1.0.11</td> <td> Disconnects on "not master" errors if <em>"safe"</em> is set. </td> </tr> <tr> <td>1.0.9</td> <td> <p class="para"> Added ability to pass integers to the <em>"safe"</em> option, which previously only accepted booleans. </p> <p class="para"> Added <em>"fsync"</em> option. </p> <p class="para"> The return type was changed to be an array containing error information if the <em>"safe"</em> option is used. Otherwise, a boolean is returned as before. </p> </td> </tr> <tr> <td>1.0.5</td> <td>Added <em>"safe"</em> option.</td> </tr> <tr> <td>1.0.1</td> <td> Changed <em><code class="parameter">options</code></em> parameter from boolean to array. Pre-1.0.1, the second parameter was an optional boolean value specifying an upsert. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-mongocollection.update-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1469"> <p><strong>Example #1 <span class="function"><strong>MongoCollection::update()</strong></span></strong></p> <div class="example-contents"><p> Adding an address field to a document. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$c</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"firstname" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"Bob"</span><span style="color: #007700">, </span><span style="color: #DD0000">"lastname" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"Jones" </span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$newdata </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'$set' </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">"address" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"1 Smith Lane"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">update</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"firstname" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"Bob"</span><span style="color: #007700">), </span><span style="color: #0000BB">$newdata</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">findOne</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"firstname" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"Bob"</span><span style="color: #007700">)));<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(4) { ["_id"]=> object(MongoId)#6 (0) { } ["firstname"]=> string(3) "Bob" ["lastname"]=> string(5) "Jones" ["address"]=> string(12) "1 Smith Lane" } </pre></div> </div> </div> <div class="example" id="example-1470"> <p><strong>Example #2 <span class="function"><strong>MongoCollection::update()</strong></span> upsert examples</strong></p> <div class="example-contents"><p> Upserts can simplify code, as a single line can create the document if it does not exist (based on <em><code class="parameter">$criteria</code></em>), or update an existing document if it matches. </p></div> <div class="example-contents"><p> In the following example, <em><code class="parameter">$new_object</code></em> contains an atomic modifier. Since the collection is empty and upsert must insert a new document, it will apply those operations to the <em><code class="parameter">$criteria</code></em> parameter in order to create the document. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$c</span><span style="color: #007700">-></span><span style="color: #0000BB">drop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">update</span><span style="color: #007700">(<br /> array(</span><span style="color: #DD0000">"uri" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"/summer_pics"</span><span style="color: #007700">),<br /> array(</span><span style="color: #DD0000">'$inc' </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">"page hits" </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">)),<br /> array(</span><span style="color: #DD0000">"upsert" </span><span style="color: #007700">=> </span><span style="color: #0000BB">true</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">findOne</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(3) { ["_id"]=> object(MongoId)#9 (0) { } ["uri"]=> string(12) "/summer_pics" ["page hits"]=> int(1) } </pre></div> </div> <div class="example-contents"><p> If <em><code class="parameter">$new_object</code></em> does not contain atomic modifiers (i.e. <em>$</em> operators), upsert will use <em><code class="parameter">$new_object</code></em> as-is for the new document. This matches the behavior of a normal update, where not using atomic modifiers causes the document to be overwritten. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$c</span><span style="color: #007700">-></span><span style="color: #0000BB">drop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">update</span><span style="color: #007700">(<br /> array(</span><span style="color: #DD0000">"name" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"joe"</span><span style="color: #007700">),<br /> array(</span><span style="color: #DD0000">"username" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"joe312"</span><span style="color: #007700">, </span><span style="color: #DD0000">"createdAt" </span><span style="color: #007700">=> new </span><span style="color: #0000BB">MongoDate</span><span style="color: #007700">()), <br /> array(</span><span style="color: #DD0000">"upsert" </span><span style="color: #007700">=> </span><span style="color: #0000BB">true</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">findOne</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(3) { ["_id"]=> object(MongoId)#10 (0) { } ["username"]=> string(6) "joe312" ["createdAt"]=> object(MongoDate)#4 (0) { } } </pre></div> </div> </div> <div class="example" id="example-1471"> <p><strong>Example #3 <span class="function"><strong>MongoCollection::update()</strong></span> multiple example</strong></p> <div class="example-contents"><p> By default, <span class="function"><strong>MongoCollection::update()</strong></span> will only update the first document matching <em><code class="parameter">$criteria</code></em> that it finds. Using the "multiple" option can override this behavior, if needed. </p></div> <div class="example-contents"><p> This example adds a "gift" field to every person whose birthday is in the next day. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$today </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'$gt' </span><span style="color: #007700">=> new </span><span style="color: #0000BB">MongoDate</span><span style="color: #007700">(), </span><span style="color: #DD0000">'$lt' </span><span style="color: #007700">=> new </span><span style="color: #0000BB">MongoDate</span><span style="color: #007700">(</span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"+1 day"</span><span style="color: #007700">)));<br /></span><span style="color: #0000BB">$people</span><span style="color: #007700">-></span><span style="color: #0000BB">update</span><span style="color: #007700">(<br /> array(</span><span style="color: #DD0000">"birthday" </span><span style="color: #007700">=> </span><span style="color: #0000BB">$today</span><span style="color: #007700">),<br /> array(</span><span style="color: #DD0000">'$set' </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">'gift' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$surprise</span><span style="color: #007700">)),<br /> array(</span><span style="color: #DD0000">"multiple" </span><span style="color: #007700">=> </span><span style="color: #0000BB">true</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-mongocollection.update-seealso"> <h3 class="title">See Also</h3> <p class="para"> The <a href="mongo.updates.html" class="link">PHP documentation on updates</a> and the <a href="http://docs.mongodb.org/manual/tutorial/modify-documents/" class="link external">» MongoDB core docs</a>. </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocollection.--tostring.html">MongoCollection::__toString</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocollection.validate.html">MongoCollection::validate</a></div> <div class="up"><a href="class.mongocollection.html">MongoCollection</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>