File: findandmodify.xml

package info (click to toggle)
php-doc 20140201-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 74,084 kB
  • ctags: 4,040
  • sloc: xml: 998,137; php: 20,812; cpp: 500; sh: 177; makefile: 63; awk: 28
file content (288 lines) | stat: -rw-r--r-- 7,796 bytes parent folder | download
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
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 330629 $ -->

<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>array</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("w" => 1)
);



$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
-->