1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 331653 $ -->
<refentry xml:id="mongocollection.update" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>MongoCollection::update</refname>
<refpurpose>Update records based on a given criteria</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>bool|array</type><methodname>MongoCollection::update</methodname>
<methodparam><type>array</type><parameter>criteria</parameter></methodparam>
<methodparam><type>array</type><parameter>new_object</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>options</parameter><initializer>array()</initializer></methodparam>
</methodsynopsis>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term>
<parameter>criteria</parameter>
</term>
<listitem>
<para>
Description of the objects to update.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<parameter>new_object</parameter>
</term>
<listitem>
<para>
The object with which to update the matching records.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<parameter>options</parameter>
</term>
<listitem>
<para>
This parameter is an associative array of the form
<literal>array("optionname" => <boolean>, ...)</literal>. Currently
supported options are:
<itemizedlist>
<listitem>
<para>
<literal>"upsert"</literal>
</para>
<para>
If no document matches <parameter>$criteria</parameter>, a new
document will be inserted.
</para>
<para>
If a new document would be inserted and
<parameter>$new_object</parameter> contains atomic modifiers
(i.e. <literal>$</literal> operators), those operations will be
applied to the <parameter>$criteria</parameter> parameter to create
the new document. If <parameter>$new_object</parameter> does not
contain atomic modifiers, it will be used as-is for the inserted
document. See the upsert examples below for more information.
</para>
</listitem>
<listitem>
<para>
<literal>"multiple"</literal>
</para>
<para>
All documents matching $criteria will be updated.
<function>MongoCollection::update</function> has exactly the opposite
behavior of <function>MongoCollection::remove</function>: it updates
one document by default, not all matching documents. <emphasis>It is
recommended that you always specify whether you want to update
multiple documents or a single document</emphasis>, as the database
may change its default behavior at some point in the future.
</para>
</listitem>
&mongo.writes.parameters.fsync;
&mongo.writes.parameters.journal;
<!--
&mongo.writes.parameters.sockettimeoutms;
-->
&mongo.writes.parameters.writeconcern;
&mongo.writes.parameters.writeconcerntimeout;
&mongo.writes.parameters.safe;
&mongo.writes.parameters.timeout;
</itemizedlist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns an array containing the status of the update if the
<literal>"w"</literal> option is set. Otherwise, returns &true;.
</para>
<para>
Fields in the status array are described in the documentation for
<function>MongoCollection::insert</function>.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
&mongo.errors.exceptions.writeconcern;
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<!--
<row>
<entry>1.5.0</entry>
<entry>Renamed the <literal>"timeout"</literal> option to
<literal>"socketTimeoutMS"</literal>.</entry>
</row>
-->
<row>
<entry>1.3.0</entry>
<entry>
The <parameter>options</parameter> parameter no longer accepts a boolean
to signify an upsert. Instead, this now has to be done with
<literal>array('upsert' => true)</literal>.
</entry>
</row>
<row>
<entry>1.2.11</entry>
<entry>
Emits <constant>E_DEPRECATED</constant> when
<parameter>options</parameter> is <type>scalar</type>.
</entry>
</row>
<row>
<entry>1.2.0</entry>
<entry>Added <literal>"timeout"</literal> option.</entry>
</row>
<row>
<entry>1.0.11</entry>
<entry>
Disconnects on "not master" errors if <literal>"safe"</literal> is set.
</entry>
</row>
<row>
<entry>1.0.9</entry>
<entry>
<para>
Added ability to pass integers to the <literal>"safe"</literal> option,
which previously only accepted booleans.
</para>
<para>
Added <literal>"fsync"</literal> option.
</para>
<para>
The return type was changed to be an array containing error information
if the <literal>"safe"</literal> option is used. Otherwise, a boolean
is returned as before.
</para>
</entry>
</row>
<row>
<entry>1.0.5</entry>
<entry>Added <literal>"safe"</literal> option.</entry>
</row>
<row>
<entry>1.0.1</entry>
<entry>
Changed <parameter>options</parameter> parameter from boolean to array.
Pre-1.0.1, the second parameter was an optional boolean value specifying
an upsert.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>MongoCollection::update</function></title>
<para>
Adding an address field to a document.
</para>
<programlisting role="php">
<![CDATA[
<?php
$c->insert(array("firstname" => "Bob", "lastname" => "Jones" ));
$newdata = array('$set' => array("address" => "1 Smith Lane"));
$c->update(array("firstname" => "Bob"), $newdata);
var_dump($c->findOne(array("firstname" => "Bob")));
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
array(4) {
["_id"]=>
object(MongoId)#6 (0) {
}
["firstname"]=>
string(3) "Bob"
["lastname"]=>
string(5) "Jones"
["address"]=>
string(12) "1 Smith Lane"
}
]]>
</screen>
</example>
<example>
<title><function>MongoCollection::update</function> upsert examples</title>
<para>
Upserts can simplify code, as a single line can create the document if it
does not exist (based on <parameter>$criteria</parameter>), or update an
existing document if it matches.
</para>
<para>
In the following example, <parameter>$new_object</parameter> contains an
atomic modifier. Since the collection is empty and upsert must insert a new
document, it will apply those operations to the
<parameter>$criteria</parameter> parameter in order to create the document.
</para>
<programlisting role="php">
<![CDATA[
<?php
$c->drop();
$c->update(
array("uri" => "/summer_pics"),
array('$inc' => array("page hits" => 1)),
array("upsert" => true)
);
var_dump($c->findOne());
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
array(3) {
["_id"]=>
object(MongoId)#9 (0) {
}
["uri"]=>
string(12) "/summer_pics"
["page hits"]=>
int(1)
}
]]>
</screen>
<para>
If <parameter>$new_object</parameter> does not contain atomic modifiers
(i.e. <literal>$</literal> operators), upsert will use
<parameter>$new_object</parameter> 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.
</para>
<programlisting role="php">
<![CDATA[
<?php
$c->drop();
$c->update(
array("name" => "joe"),
array("username" => "joe312", "createdAt" => new MongoDate()),
array("upsert" => true)
);
var_dump($c->findOne());
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
array(3) {
["_id"]=>
object(MongoId)#10 (0) {
}
["username"]=>
string(6) "joe312"
["createdAt"]=>
object(MongoDate)#4 (0) {
}
}
]]>
</screen>
</example>
<example>
<title><function>MongoCollection::update</function> multiple example</title>
<para>
By default, <function>MongoCollection::update</function> will only update
the first document matching <parameter>$criteria</parameter> that it
finds. Using the "multiple" option can override this behavior, if needed.
</para>
<para>
This example adds a "gift" field to every person whose birthday is in the
next day.
</para>
<programlisting role="php">
<![CDATA[
<?php
$today = array('$gt' => new MongoDate(), '$lt' => new MongoDate(strtotime("+1 day")));
$people->update(
array("birthday" => $today),
array('$set' => array('gift' => $surprise)),
array("multiple" => true)
);
?>
]]>
</programlisting>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
The <link linkend="mongo.updates">PHP documentation on updates</link> and the
<link xlink:href="&url.mongodb.dochub.update;">MongoDB core docs</link>.
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|