php-doc-en/reference/mongo/mongocollection/insert.xml
2014-03-10 15:03:19 +00:00

431 lines
12 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<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.writeconcerntimeoutms;
&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>"wtimeout"</literal> option to
<literal>"wTimeoutMS"</literal>.</entry>
</row>
<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
-->