File: insert.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 (429 lines) | stat: -rw-r--r-- 11,771 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?xml version="1.0" encoding="utf-8"?>

<!-- $Revision: 330300 $ -->
<refentry xml:id="mongocollection.insert" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <refnamediv>
  <refname>MongoCollection::insert</refname>
  <refpurpose>Inserts a document into the collection</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <modifier>public</modifier> <type>bool|array</type><methodname>MongoCollection::insert</methodname>
   <methodparam><type>array|object</type><parameter>a</parameter></methodparam>
   <methodparam choice="opt"><type>array</type><parameter>options</parameter><initializer>array()</initializer></methodparam>
  </methodsynopsis>
  <para>
   All strings sent to the database must be UTF-8.  If a string is not UTF-8, a 
   <classname>MongoException</classname> will be thrown.  To insert 
   (or query for) a non-UTF-8 string, use <classname>MongoBinData</classname>.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term>
      <parameter>a</parameter>
     </term>
     <listitem>
      <para>
       An array or object. If an object is used, it may not have protected or
       private properties.
      </para>
      <note>
       <para>
        If the parameter does not have an <literal>_id</literal> key or
        property, a new <classname>MongoId</classname> instance will be created
        and assigned to it. This special behavior does not mean that the
        parameter is passed by reference.
       </para>
      </note>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      <parameter>options</parameter>
     </term>
     <listitem>
      <para>
       Options for the insert.
       <itemizedlist>
        &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 insertion if the
   <literal>"w"</literal> option is set. Otherwise, returns &true; if the
   inserted array is not empty (a <classname>MongoException</classname> will be
   thrown if the inserted array is empty).
  </para>
  <para>
   If an array is returned, the following keys may be present:
   <variablelist>
    <varlistentry>
     <term>
      <parameter>ok</parameter>
     </term>
     <listitem>
      <para>
       This should almost always be 1 (unless last_error itself failed).
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      <parameter>err</parameter>
     </term>
     <listitem>
      <para>
       If this field is non-null, an error occurred on the previous operation.
       If this field is set, it will be a string describing the error that
       occurred. 
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      <parameter>code</parameter>
     </term>
     <listitem>
      <para>
       If a database error occurred, the relevant error code will be passed
       back to the client. 
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      <parameter>errmsg</parameter>
     </term>
     <listitem>
      <para>
       This field is set if something goes wrong with a database command. It
       is coupled with <literal>ok</literal> being 0. For example, if
       <literal>w</literal> is set and times out, errmsg will be set to "timed
       out waiting for slaves" and <literal>ok</literal> will be 0.  If this
       field is set, it will be a string describing the error that occurred. 
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      <parameter>n</parameter>
     </term>
     <listitem>
      <para>
       If the last operation was an update, upsert, or a remove, the number
       of documents affected will be returned. For insert operations, this value
       is always <literal>0</literal>.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      <parameter>wtimeout</parameter>
     </term>
     <listitem>
      <para>
       If the previous option timed out waiting for replication. 
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      <parameter>waited</parameter>
     </term>
     <listitem>
      <para>
       How long the operation waited before timing out. 
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      <parameter>wtime</parameter>
     </term>
     <listitem>
      <para>
       If <literal>w</literal> was set and the operation succeeded, how long it took to
       replicate to <literal>w</literal> servers. 
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      <parameter>upserted</parameter>
     </term>
     <listitem>
      <para>
       If an upsert occurred, this field will contain the new record's
       <literal>_id</literal> field. For upserts, either this field or
       <literal>updatedExisting</literal> will be present (unless an error
       occurred). 
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term>
      <parameter>updatedExisting</parameter>
     </term>
     <listitem>
      <para>
       If an upsert updated an existing element, this field will be true. For
       upserts, either this field or upserted will be present (unless an error
       occurred). 
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>
 </refsect1>

 <refsect1 role="errors">
  &reftitle.errors;
  <para>
   Throws <classname>MongoException</classname> if the inserted document is
   empty or if it contains zero-length keys. Attempting to insert an object with
   protected and private properties will cause a zero-length key error.
  </para>
  &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 a acknowledged write. Instead, this now has to be done with
        <literal>array('w' => 1)</literal> (The default behaviour of
        <classname>MongoClient</classname>).
       </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>
        Changed second parameter to be an array of options. Pre-1.0.5, the
        second parameter was a boolean indicating the <literal>"safe"</literal>
        option.
       </entry>
      </row>
      <row>
       <entry>1.0.1</entry>
       <entry>
        Throw a <classname>MongoCursorException</classname> if the
        <literal>"safe"</literal> option is set and the insert fails.
       </entry>
      </row>
     </tbody>
    </tgroup>
   </informaltable>
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <example>
   <title><function>MongoCollection::insert</function> <literal>_id</literal> example</title>
   <para>
    An <literal>_id</literal> field will be added to the inserted document if
    not already present. Depending on how the parameter is passed, a generated
    <literal>_id</literal> may or may not be available to calling code.
   </para>
   <programlisting role="php">
<![CDATA[
<?php

$m = new MongoClient();
$collection = $m->selectCollection('test', 'phpmanual');

// If an array literal is used, there is no way to access the generated _id
$collection->insert(array('x' => 1));

// The _id is available on an array passed by value
$a = array('x' => 2);
$collection->insert($a);
var_dump($a);

// The _id is not available on an array passed by reference
$b = array('x' => 3);
$ref = &$b;
$collection->insert($ref);
var_dump($ref);

// The _id is available if a wrapping function does not trigger copy-on-write
function insert_no_cow($collection, $document)
{
    $collection->insert($document);
}

$c = array('x' => 4);
insert_no_cow($collection, $c);
var_dump($c);

// The _id is not available if a wrapping function triggers copy-on-write
function insert_cow($collection, $document)
{
    $document['y'] = 1;
    $collection->insert($document);
}

$d = array('x' => 5);
insert_cow($collection, $d);
var_dump($d);

?>
]]>
   </programlisting>
   &example.outputs.similar;
   <screen>
<![CDATA[
array(2) {
  ["x"]=>
  int(2)
  ["_id"]=>
  object(MongoId)#4 (0) {
  }
}
array(1) {
  ["x"]=>
  int(3)
}
array(2) {
  ["x"]=>
  int(4)
  ["_id"]=>
  object(MongoId)#5 (0) {
  }
}
array(1) {
  ["x"]=>
  int(5)
}
]]>
   </screen>
  </example>

  <example>
   <title><function>MongoCollection::insert</function> acknowledged write example</title>
   <para>
    This example shows inserting two elements with the same _id, which causes 
    a <classname>MongoCursorException</classname> to be thrown, as 
    <parameter>w</parameter> was set.
   </para>
   <programlisting role="php">
<![CDATA[
<?php

$person = array("name" => "Joe", "age" => 20);
$collection->insert($person);

// now $person has an _id field, so if we save it 
// again, we will get an exception
try {
    $collection->insert($person, array("w" => 1));
} catch(MongoCursorException $e) {
    echo "Can't save the same person twice!\n";
}

?>
]]>
   </programlisting>
  </example>
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <simplelist>
   <member><function>MongoCollection::batchInsert</function></member>
   <member><function>MongoCollection::update</function></member>
   <member><function>MongoCollection::find</function></member>
   <member><function>MongoCollection::remove</function></member>
   <member>MongoDB core docs on <link xlink:href="&url.mongodb.dochub.insert;">insert</link>.</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
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
-->