Sophie

Sophie

distrib > Mageia > 7 > armv7hl > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 9973

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!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 &gt;=0.9.0)</p><p class="refpurpose"><span class="refname">MongoCollection::update</span> &mdash; <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 id="mongocollection.update.criteria">
      <code class="parameter">criteria</code>
     </dt>

     <dd>

      <p class="para">
       Query criteria for the documents to update.
      </p>
     </dd>

    
    
     <dt id="mongocollection.update.new_object">
      <code class="parameter">new_object</code>
     </dt>

     <dd>

      <p class="para">
       The object used to update the matched documents. This may either contain
       update operators (for modifying specific fields) or be a replacement
       document.
      </p>
     </dd>

    
    
     <dt>

      <code class="parameter">options</code>
     </dt>

     <dd>

      <p class="para">
       An array of options for the update operation. Currently available options
       include:
       <ul class="itemizedlist">
        <li class="listitem" id="mongocollection.update.upsert">
         <p class="para">
          <em>&quot;upsert&quot;</em>
         </p>
         <p class="para">
          If no document matches <code class="parameter">$criteria</code>, a new
          document will be inserted.
         </p>
         <p class="para">
          If a new document would be inserted and
          <code class="parameter">$new_object</code> contains atomic modifiers
          (i.e. <em>$</em> operators), those operations will be
          applied to the <code class="parameter">$criteria</code> parameter to create
          the new document. If <code class="parameter">$new_object</code> 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" id="mongocollection.update.multiple">
         <p class="para">
          <em>&quot;multiple&quot;</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>&quot;fsync&quot;</em></p><p class="para">Boolean, defaults to <strong><code>FALSE</code></strong>. If journaling is enabled, it works exactly like <em>&quot;j&quot;</em>. If journaling is not enabled, the write operation blocks until it is synced to database files on disk. If <strong><code>TRUE</code></strong>, an acknowledged insert is implied and this option will override setting <em>&quot;w&quot;</em> to <em>0</em>.</p><blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">If journaling is enabled, users are strongly encouraged to use the <em>&quot;j&quot;</em> option instead of <em>&quot;fsync&quot;</em>. Do not use <em>&quot;fsync&quot;</em> and <em>&quot;j&quot;</em> simultaneously, as that will result in an error.</span></p></blockquote></li>
        <li class="listitem"><p class="para"><em>&quot;j&quot;</em></p><p class="para">Boolean, defaults to <strong><code>FALSE</code></strong>. Forces the write operation to block until it is synced to the journal on disk. If <strong><code>TRUE</code></strong>, an acknowledged write is implied and this option will override setting <em>&quot;w&quot;</em> to <em>0</em>.</p><blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">If this option is used and journaling is disabled, MongoDB 2.6+ will raise an error and the write will fail; older server versions will simply ignore the option.</span></p></blockquote></li>
        <li class="listitem"><p class="para"><em>&quot;socketTimeoutMS&quot;</em></p><p class="para">This option specifies the time limit, in milliseconds, for socket communication. If the server does not respond within the timeout period, a <a href="class.mongocursortimeoutexception.html" class="classname">MongoCursorTimeoutException</a> will be thrown and there will be no way to determine if the server actually handled the write or not. A value of <em>-1</em> may be specified to block indefinitely. The default value for <a href="class.mongoclient.html" class="classname">MongoClient</a> is <em>30000</em> (30 seconds).</p></li>
        <li class="listitem"><p class="para"><em>&quot;w&quot;</em></p><p class="para">See <a href="mongo.writeconcerns.html" class="link">Write Concerns</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>&quot;wTimeoutMS&quot;</em></p><p class="para">This option specifies the time limit, in milliseconds, for <a href="mongo.writeconcerns.html" class="link">write concern</a> acknowledgement. It is only applicable when <em>&quot;w&quot;</em> is greater than <em>1</em>, as the timeout pertains to replication. If the write concern is not satisfied within the time limit, a <a href="class.mongocursorexception.html" class="classname">MongoCursorException</a> will be thrown. A value of <em>0</em> may be specified to block indefinitely. The default value for <a href="class.mongoclient.html" class="classname">MongoClient</a> is <em>10000</em> (ten seconds).</p></li>
       </ul>
      </p>
      <p class="para">
       The following options are deprecated and should no longer be used:
       <ul class="itemizedlist">
        <li class="listitem"><p class="para"><em>&quot;safe&quot;</em></p><p class="para">Deprecated. Please use the <a href="mongo.writeconcerns.html" class="link">write concern</a> <em>&quot;w&quot;</em> option.</p></li>
        <li class="listitem"><p class="para"><em>&quot;timeout&quot;</em></p><p class="para">Deprecated alias for <em>&quot;socketTimeoutMS&quot;</em>.</p></li>
        <li class="listitem"><p class="para"><em>&quot;wtimeout&quot;</em></p><p class="para">Deprecated alias for <em>&quot;wTimeoutMS&quot;</em>.</p></li>
       </ul>
      </p>
     </dd>

    
   </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>&quot;w&quot;</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 <em>&quot;w&quot;</em> option is set and the write fails.</p><p class="para">Throws <a href="class.mongocursortimeoutexception.html" class="classname">MongoCursorTimeoutException</a> if the <em>&quot;w&quot;</em> 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 <var class="varname"><var class="varname">MongoCollection::$wtimeout</var></var> 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.5.0</td>
       <td>
        <p class="para">
         Added the <em>&quot;wTimeoutMS&quot;</em> option, which replaces
         <em>&quot;wtimeout&quot;</em>. Emits <strong><code>E_DEPRECATED</code></strong>
         when <em>&quot;wtimeout&quot;</em> is used.
        </p>
        <p class="para">
         Added the <em>&quot;socketTimeoutMS&quot;</em> option, which replaces
         <em>&quot;timeout&quot;</em>. Emits <strong><code>E_DEPRECATED</code></strong>
         when <em>&quot;timeout&quot;</em> is used.
        </p>
        <p class="para">
         Emits <strong><code>E_DEPRECATED</code></strong> when <em>&quot;safe&quot;</em>
         is used.
        </p>
       </td>
      </tr>

      <tr>
       <td>1.3.4</td>
       <td>Added <em>&quot;wtimeout&quot;</em> option.</td>
      </tr>

      <tr>
       <td>1.3.0</td>
       <td>
        <p class="para">Added <em>&quot;w&quot;</em> option.</p>
        <p class="para">
         The <code class="parameter">options</code> parameter no longer accepts a boolean
         to signify an upsert. Instead, this now has to be done with
         <em>array(&#039;upsert&#039; =&gt; true)</em>.
        </p>
       </td>
      </tr>

      <tr>
       <td>1.2.11</td>
       <td>
        Emits <strong><code>E_DEPRECATED</code></strong> when
        <code class="parameter">options</code> is <span class="type"><span class="type scalar">scalar</span></span>.
       </td>
      </tr>

      <tr>
       <td>1.2.0</td>
       <td>Added <em>&quot;timeout&quot;</em> option.</td>
      </tr>

      <tr>
       <td>1.0.11</td>
       <td>
        Disconnects on &quot;not master&quot; errors if <em>&quot;safe&quot;</em> is set.
       </td>
      </tr>

      <tr>
       <td>1.0.9</td>
       <td>
        <p class="para">
         Added ability to pass integers to the <em>&quot;safe&quot;</em> option,
         which previously only accepted booleans.
        </p>
        <p class="para">
         Added <em>&quot;fsync&quot;</em> option.
        </p>
        <p class="para">
         The return type was changed to be an array containing error information
         if the <em>&quot;safe&quot;</em> option is used. Otherwise, a boolean
         is returned as before.
        </p>
       </td>
      </tr>

      <tr>
       <td>1.0.5</td>
       <td>Added <em>&quot;safe&quot;</em> option.</td>
      </tr>

      <tr>
       <td>1.0.1</td>
       <td>
        Changed <code class="parameter">options</code> 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-1610">
   <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">&lt;?php<br /><br />$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"firstname"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Bob"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"lastname"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Jones"&nbsp;</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$newdata&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'$set'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"address"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"1&nbsp;Smith&nbsp;Lane"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">update</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"firstname"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Bob"</span><span style="color: #007700">),&nbsp;</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">-&gt;</span><span style="color: #0000BB">findOne</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"firstname"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Bob"</span><span style="color: #007700">)));<br /><br /></span><span style="color: #0000BB">?&gt;</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) {
  [&quot;_id&quot;]=&gt;
  object(MongoId)#6 (0) {
  }
  [&quot;firstname&quot;]=&gt;
  string(3) &quot;Bob&quot;
  [&quot;lastname&quot;]=&gt;
  string(5) &quot;Jones&quot;
  [&quot;address&quot;]=&gt;
  string(12) &quot;1 Smith Lane&quot;
}
</pre></div>
   </div>
  </div>
  <div class="example" id="example-1611">
   <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 <code class="parameter">$criteria</code>), or update an
    existing document if it matches.
   </p></div>
   <div class="example-contents"><p>
    In the following example, <code class="parameter">$new_object</code> contains an
    atomic modifier. Since the collection is empty and upsert must insert a new
    document, it will apply those operations to the
    <code class="parameter">$criteria</code> 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">&lt;?php<br /><br />$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">drop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">update</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"uri"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"/summer_pics"</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'$inc'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"page&nbsp;hits"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"upsert"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</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">-&gt;</span><span style="color: #0000BB">findOne</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">?&gt;</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) {
  [&quot;_id&quot;]=&gt;
  object(MongoId)#9 (0) {
  }
  [&quot;uri&quot;]=&gt;
  string(12) &quot;/summer_pics&quot;
  [&quot;page hits&quot;]=&gt;
  int(1)
}
</pre></div>
   </div>
   <div class="example-contents"><p>
    If <code class="parameter">$new_object</code> does not contain atomic modifiers
    (i.e. <em>$</em> operators), upsert will use
    <code class="parameter">$new_object</code> 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">&lt;?php<br /><br />$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">drop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">update</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"joe"</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"username"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"joe312"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"createdAt"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDate</span><span style="color: #007700">()),&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"upsert"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</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">-&gt;</span><span style="color: #0000BB">findOne</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">?&gt;</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) {
  [&quot;_id&quot;]=&gt;
  object(MongoId)#10 (0) {
  }
  [&quot;username&quot;]=&gt;
  string(6) &quot;joe312&quot;
  [&quot;createdAt&quot;]=&gt;
  object(MongoDate)#4 (0) {
  }
}
</pre></div>
   </div>
  </div>
  <div class="example" id="example-1612">
   <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 <code class="parameter">$criteria</code> that it
    finds. Using the &quot;multiple&quot; option can override this behavior, if needed.
   </p></div>
   <div class="example-contents"><p>
    This example adds a &quot;gift&quot; 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">&lt;?php<br /><br />$today&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'$gt'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDate</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">'$lt'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</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&nbsp;day"</span><span style="color: #007700">)));<br /></span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">update</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"birthday"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$today</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'$set'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'gift'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$surprise</span><span style="color: #007700">)),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"multiple"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">?&gt;</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="https://docs.mongodb.com/manual/tutorial/modify-documents/" class="link external">&raquo;&nbsp;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>