Document MongoCollection->findAndModify()

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@328209 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Hannes Magnusson 2012-10-31 23:49:20 +00:00
parent d2a496cb22
commit 206dda18e9

View file

@ -0,0 +1,288 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mongocollection.findandmodify" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>MongoCollection::findAndModify</refname>
<refpurpose>Update a document and return it</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>void</type><methodname>MongoCollection::findAndModify</methodname>
<methodparam><type>array</type><parameter>query</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>update</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>fields</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>options</parameter></methodparam>
</methodsynopsis>
<para>
The findAndModify command atomically modifies and returns a single document.
By default, the returned document does not include the modifications made on
the update. To return the document with the modifications made on the
update, use the <varname>new</varname> option.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>query</parameter></term>
<listitem>
<para>
The query criteria to search for.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>update</parameter></term>
<listitem>
<para>
The update criteria.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>fields</parameter></term>
<listitem>
<para>
Optionally only return these fields.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>options</parameter></term>
<listitem>
<para>
An array of options to apply, such as remove the match document from the
DB and return it.
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>Option</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry><varname>sort</varname> <type>array</type></entry>
<entry>
Determines which document the operation will modify if the
query selects multiple documents. findAndModify will modify the
first document in the sort order specified by this argument.
</entry>
</row>
<row>
<entry><varname>remove</varname> <type>boolean</type></entry>
<entry>
Optional if <varname>update</varname> field exists. When &true;, removes the selected
document. The default is &false;.
</entry>
</row>
<row>
<entry><varname>update</varname> <type>array</type></entry>
<entry>
Optional if <varname>remove</varname> field exists.
Performs an update of the selected document.
</entry>
</row>
<row>
<entry><varname>new</varname> <type>boolean</type></entry>
<entry>
Optional. When &true;, returns the modified document rather than the
original. The findAndModify method ignores the <varname>new</varname> option for
remove operations. The default is &false;.
</entry>
</row>
<row>
<entry><varname>upsert</varname> <type>boolean</type></entry>
<entry>
Optional. Used in conjunction with the <varname>update</varname> field. When &true;, the
findAndModify command creates a new document if the query returns
no documents. The default is false. In MongoDB 2.2, the
findAndModify command returns &null; when upsert is &true;.
</entry>
</row>
<row>
<entry><varname></varname></entry>
<entry>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the original document, or the modified document when
<varname>new</varname> is set.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
Throws <classname>MongoResultException</classname> on failure.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example xml:id="mongocollection.findandmodify.example.basic">
<title><methodname>MongoCollection::findAndModify</methodname> example</title>
<programlisting role="php">
<![CDATA[
<?php
$m = new Mongo;
$col = $m->selectDB("test")->jobs;
$col->insert(array(
"name" => "Next promo",
"inprogress" => false,
"priority" => 0,
"tasks" => array( "select product", "add inventory", "do placement"),
) );
$col->insert(array(
"name" => "Biz report",
"inprogress" => false,
"priority" => 1,
"tasks" => array( "run sales report", "email report" )
) );
$col->insert(array(
"name" => "Biz report",
"inprogress" => false,
"priority" => 2,
"tasks" => array( "run marketing report", "email report" )
),
array("safe" => true)
);
$retval = $col->findAndModify(
array("inprogress" => false, "name" => "Biz report"),
array('$set' => array('inprogress' => true, "started" => new MongoDate())),
null,
array(
"sort" => array("priority" => -1),
"new" => true,
)
);
var_dump($retval);
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
array(6) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "5091b5b244415e8cc3000002"
}
["inprogress"]=>
bool(true)
["name"]=>
string(10) "Biz report"
["priority"]=>
int(2)
["started"]=>
object(MongoDate)#8 (2) {
["sec"]=>
int(1351726514)
["usec"]=>
int(925000)
}
["tasks"]=>
array(2) {
[0]=>
string(20) "run marketing report"
[1]=>
string(12) "email report"
}
}
]]>
</screen>
</example>
<example xml:id="mongocollection.findandmodify.example.error">
<title><methodname>MongoCollection::findAndModify</methodname> error handling</title>
<programlisting role="php">
<![CDATA[
<?php
$m = new Mongo;
$col = $m->selectDB("test")->jobs;
try {
$retval = $col->findAndModify(
array("inprogress" => false, "name" => "Next promo"),
array('$pop' => array("tasks" => -1)),
array("tasks" => array('$pop' => array("stuff"))),
array("new" => true)
);
} catch(MongoResultException $e) {
echo $e->getCode(), " : ", $e->getMessage(), "\n";
var_dump($e->getDocument());
}
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
13097 : exception: Unsupported projection option: $pop
array(3) {
["errmsg"]=>
string(46) "exception: Unsupported projection option: $pop"
["code"]=>
int(13097)
["ok"]=>
float(0)
}
]]>
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member>The MongoDB <link xlink:href="&url.mongodb.docs.command;findAndModify/">findAndModify command</link> docs</member>
</simplelist>
</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
indent-tabs-mode:nil
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
-->