php-doc-en/reference/mongo/mongocollection/update.xml
2010-11-17 08:35:57 +00:00

340 lines
9.4 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<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>boolean</type><methodname>MongoCollection::update</methodname>
<methodparam><type>array</type><parameter>criteria</parameter></methodparam>
<methodparam><type>array</type><parameter>newobj</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>newobj</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" => &lt;boolean&gt;, ...)</literal>. Currently
supported options are:
<itemizedlist>
<listitem>
<para>
<literal>"upsert"</literal>
</para>
<para>
If no document matches $criteria, a new document will be created from
$criteria and $newobj (see upsert example below).
</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>
<listitem>
<para>
<literal>"safe"</literal>
</para>
<para>
Can be a boolean or integer, defaults to &false;. If &false;, the
program continues executing without waiting for a database response.
If &true;, the program will wait for the database response and throw a
<classname>MongoCursorException</classname> if the update did not
succeed.
</para>
<para>
If you are using replication and the master has changed, using "safe"
will make the driver disconnect from the master, throw and exception,
and attempt to find a new master on the next operation (your
application must decide whether or not to retry the operation on the
new master).
</para>
<para>
If you <emphasis>do not</emphasis> use "safe" with a replica set and
the master changes, there will be no way for the driver to know about
the change so it will continuously and silently fail to write.
</para>
<para>
If <literal>safe</literal> is an integer, will replicate the
update to that many machines before returning success (or throw an
exception if the replication times out, see wtimeout). This overrides
the w variable set on the collection.
</para>
</listitem>
<listitem>
<para>
<literal>"fsync"</literal>
</para>
<para>
Boolean, defaults to &false;. Forces the update to be synced to
disk before returning success. If &true;, a safe update is implied
and will override setting <literal>safe</literal> to &false;.
</para>
</listitem>
</itemizedlist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns if the update was successfully sent to the database.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
Throws <classname>MongoCursorException</classname> if the "safe" option is
set and the update fails.
</para>
<para>
Throws <classname>MongoCursorTimeoutException</classname> if the "safe"
option is set to a value greater than one and the database cannot replicate
the operation in <literal>MongoCollection::$wtimeout</literal> milliseconds.
</para>
</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.0.1</entry>
<entry>
Changed "options" parameter from boolean to array. Pre-1.0.1, the
second parameter was an optional boolean value specifying an upsert.
</entry>
</row>
<row>
<entry>1.0.5</entry>
<entry>
Added "safe" option.
</entry>
</row>
<row>
<entry>1.0.9</entry>
<entry>
Added ability to pass integers to "safe" options (only accepted booleans
before) and added "fsync" option.
</entry>
</row>
<row>
<entry>1.0.11</entry>
<entry>
Disconnects on "not master" errors if "safe" is set.
</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 example</title>
<para>
Upserts can simplify code, as a single line can create the object if it does
not exist yet and update it if it does.
</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 <literal>newobj</literal> does not contain $-operators, an upsert will
create a new document from it alone. This matches the behavior of a normal
update, where not using $-operators causes the whole document to be
overwritten.
</para>
<programlisting role="php">
<![CDATA[
<?php
$c->update(array("name" => "joe"), array("username" => "joe312", "createdAt" => new MongoDate()),
array("upsert" => true));
?>
]]>
</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 $criteria 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
-->