mirror of
https://github.com/php/doc-es.git
synced 2026-03-25 16:02:13 +01:00
git-svn-id: https://svn.php.net/repository/phpdoc/es/trunk@336506 c90b9560-bf6c-de11-be94-00142212c4b1
397 lines
12 KiB
XML
397 lines
12 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 13445ae645cc80ee9b69e10b8b3f94070b8d865b Maintainer: seros Status: ready -->
|
|
<!-- Reviewed: no Maintainer: andresdzphp -->
|
|
|
|
<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>Actualizar registros basándose en los criterios proporcionados</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<modifier>public</modifier> <type>bool|array</type><methodname>MongoCollection::update</methodname>
|
|
<methodparam><type>array</type><parameter>criteria</parameter></methodparam>
|
|
<methodparam><type>array</type><parameter>new_object</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 xml:id="mongocollection.update.criteria">
|
|
<term>
|
|
<parameter>criteria</parameter>
|
|
</term>
|
|
<listitem>
|
|
<para>
|
|
Criterios de consulta para los documentos a actualizar.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry xml:id="mongocollection.update.new_object">
|
|
<term>
|
|
<parameter>new_object</parameter>
|
|
</term>
|
|
<listitem>
|
|
<para>
|
|
El objeto usado para actualizar los documentos coincidentes. Podría contener
|
|
operadores de actualización (para modificar campos específicos) o ser un documento
|
|
de remplazo.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term>
|
|
<parameter>options</parameter>
|
|
</term>
|
|
<listitem>
|
|
<para>
|
|
Un array de opciones para la operación de actualización. Las opciones disponibles
|
|
actualmente son:
|
|
<itemizedlist>
|
|
<listitem xml:id="mongocollection.update.upsert">
|
|
<para>
|
|
<literal>"upsert"</literal>
|
|
</para>
|
|
<para>
|
|
Si ningún documento cumple las condiciones de <parameter>$criteria</parameter>, se
|
|
insertará un nuevo documento.
|
|
</para>
|
|
<para>
|
|
Si se insertara un nuevo documento y
|
|
<parameter>$new_object</parameter> contiene modificadores atómicos
|
|
(esto es, operadores <literal>$</literal>), esas operaciones serán
|
|
aplicadas al parámetro <parameter>$criteria</parameter> para crear
|
|
el nuevo documento. Si <parameter>$new_object</parameter> no
|
|
contiene modificadores atómicos, será utilizado tal cual para el documento
|
|
insterdo. Véase los ejemplos de upsert abajo para más información.
|
|
</para>
|
|
</listitem>
|
|
<listitem xml:id="mongocollection.update.multiple">
|
|
<para>
|
|
<literal>"multiple"</literal>
|
|
</para>
|
|
<para>
|
|
Todos los documentos que cumplan las condiciones de $criteria se actualizarán.
|
|
<function>MongoCollection::update</function> tiene exactamente el comportamiento
|
|
contrario que <function>MongoCollection::remove</function>: de forma predeterminada,
|
|
actualiza sólo un documento, no todos los que cumplan las condiciones. <emphasis>Se
|
|
recomienda especificar siempre se si deben actualizar múltiples
|
|
documentos o un único documento</emphasis>, ya que el comportamiento predeterminado de
|
|
la base de datos podría cambiar en el futuro.
|
|
</para>
|
|
</listitem>
|
|
&mongo.writes.parameters.fsync;
|
|
&mongo.writes.parameters.journal;
|
|
&mongo.writes.parameters.sockettimeoutms;
|
|
&mongo.writes.parameters.writeconcern;
|
|
&mongo.writes.parameters.writeconcerntimeoutms;
|
|
</itemizedlist>
|
|
</para>
|
|
<para>
|
|
Las siguientes opciones están obsoletas y no deberían usarse más:
|
|
<itemizedlist>
|
|
&mongo.writes.parameters.safe;
|
|
&mongo.writes.parameters.timeout;
|
|
&mongo.writes.parameters.writeconcerntimeout;
|
|
</itemizedlist>
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Devuelve un array que contiene el estado de la actualización si la
|
|
opción <literal>"w"</literal> está establecida. De lo contrario, devuelve &true;.
|
|
</para>
|
|
<para>
|
|
Los campos del array de estado están descritos en la documentación para
|
|
<function>MongoCollection::insert</function>.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="errors">
|
|
&reftitle.errors;
|
|
&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>
|
|
<para>
|
|
se añadió la opción <literal>"wTimeoutMS"</literal>, la cual remplaza a
|
|
<literal>"wtimeout"</literal>. Emite un error de nivel <constant>E_DEPRECATED</constant>
|
|
cuando se usa <literal>"wtimeout"</literal>.
|
|
</para>
|
|
<para>
|
|
Se añadió la opción <literal>"socketTimeoutMS"</literal>, la cual remplza a
|
|
<literal>"timeout"</literal>. Emite un error de nivel <constant>E_DEPRECATED</constant>
|
|
cuando se usa <literal>"timeout"</literal>.
|
|
</para>
|
|
<para>
|
|
Emite un error de nivel <constant>E_DEPRECATED</constant> al
|
|
usar <literal>"safe"</literal>.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>1.3.4</entry>
|
|
<entry>Se añadió la opción <literal>"wtimeout"</literal>.</entry>
|
|
</row>
|
|
<row>
|
|
<entry>1.3.0</entry>
|
|
<entry>
|
|
<para>Se añadió la opción <literal>"w"</literal>.</para>
|
|
<para>
|
|
El parámetro <parameter>options</parameter> ya no acepta un valor booleano
|
|
para indicar un 'upsert'. En su lugar, esto ahora se tiene que hacer
|
|
con <literal>array('upsert' => true)</literal>.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>1.2.11</entry>
|
|
<entry>
|
|
Emite <constant>E_DEPRECATED</constant> cuando
|
|
<parameter>options</parameter> es de tipo <type>scalar</type>.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>1.2.0</entry>
|
|
<entry>Se añadió la opción <literal>"timeout"</literal>.</entry>
|
|
</row>
|
|
<row>
|
|
<entry>1.0.11</entry>
|
|
<entry>
|
|
Se desconecta en errores "not master" si <literal>"safe"</literal> está establecido.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>1.0.9</entry>
|
|
<entry>
|
|
<para>
|
|
Añadida la capacidad de pasar enteros a la opción <literal>"safe"</literal>,
|
|
la cual anteriormente únicamente aceptaba booleanos.
|
|
</para>
|
|
<para>
|
|
Añadida la opción <literal>"fsync"</literal>.
|
|
</para>
|
|
<para>
|
|
Cambiado el tipo devuelto por un array que contiene información
|
|
si se utiliza la opción <literal>"safe"</literal>. De otro modo, se devuelve
|
|
un booleano como antes.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>1.0.5</entry>
|
|
<entry>Se añadió la opción <literal>"safe"</literal>.</entry>
|
|
</row>
|
|
<row>
|
|
<entry>1.0.1</entry>
|
|
<entry>
|
|
Cambiado el parámetro <parameter>"opciones"</parameter> de un booleano a un array.
|
|
Antes de la versión 1.0.1, el segundo parámetro era valor booleano opcional especificando
|
|
un upsert.
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<example>
|
|
<title><function>MongoCollection::update</function></title>
|
|
<para>
|
|
Añadiendo el campo dirección a un documento
|
|
</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
$c->insert(array("nombre" => "Pedro", "apellido" => "Ruiz" ));
|
|
$nuevosdatos = array('$set' => array("direccion" => "Calle Juan, 1"));
|
|
$c->update(array("nombre" => "Pedro"), $nuevosdatos);
|
|
|
|
var_dump($c->findOne(array("nombre" => "Pedro")));
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs.similar;
|
|
<screen>
|
|
<![CDATA[
|
|
array(4) {
|
|
["_id"]=>
|
|
object(MongoId)#6 (0) {
|
|
}
|
|
["nombre"]=>
|
|
string(3) "Pedro"
|
|
["apellido"]=>
|
|
string(5) "Ruiz"
|
|
["direccion"]=>
|
|
string(12) "Calle Juan, 1"
|
|
}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title>Ejemplos de <function>MongoCollection::update</function> con upsert</title>
|
|
<para>
|
|
Los upserts pueden simplificar el código, ya que con una única línea se puede crear el documento si
|
|
no existe (basado en <parameter>$criteria</parameter>), o actualizar un
|
|
documento existente si coincide.
|
|
</para>
|
|
<para>
|
|
En el siguiente ejemplo, <parameter>$new_object</parameter> contiene un
|
|
modificador atómico. Ya que la colección está vacía y upsert debe insertar un nuevo
|
|
documento, aplicará esas operaciones al
|
|
parámetro <parameter>$criteria</parameter> para crear el documento.
|
|
</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) "/fotos_verano"
|
|
["accesos"]=>
|
|
int(1)
|
|
}
|
|
]]>
|
|
</screen>
|
|
<para>
|
|
Si <parameter>$new_object</parameter> no contiene modificadores atómicos
|
|
(esto es, operadores <literal>$</literal>), upsert utilizará
|
|
<parameter>$new_object</parameter> tal cual para el nuevo documento. Esto coincide
|
|
con el comportamiento de una actualización normal, donde la no utilización de modificadores atómicos
|
|
ocasiona la sobrescritura del documento.
|
|
</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
$c->drop();
|
|
$c->update(
|
|
array("name" => "joe"),
|
|
array("username" => "joe312", "createdAt" => new MongoDate()),
|
|
array("upsert" => true)
|
|
);
|
|
var_dump($c->findOne());
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs.similar;
|
|
<screen>
|
|
<![CDATA[
|
|
array(3) {
|
|
["_id"]=>
|
|
object(MongoId)#10 (0) {
|
|
}
|
|
["usuario"]=>
|
|
string(6) "juan312"
|
|
["fechaAlta"]=>
|
|
object(MongoDate)#4 (0) {
|
|
}
|
|
}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title>Ejemplo de múltiples <function>MongoCollection::update</function></title>
|
|
<para>
|
|
De forma predeterminada, <function>MongoCollection::update</function> sólo
|
|
actualizará el primer documento que encuentre que cumpla las condiciones de <parameter>$criteria</parameter>. Si fuera
|
|
necesario, mediante la opción "multiple" podremos sobrescribir este comportamiento.
|
|
</para>
|
|
<para>
|
|
Este ejemplo añade un campo "regalo" a cada persona cuyo cumpleaños sea
|
|
el próximo día.
|
|
</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>
|
|
La <link linkend="mongo.updates">documentación de PHP sobre actualizaciones</link> y la
|
|
<link xlink:href="&url.mongodb.dochub.update;">documentacion en MongoDB</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
|
|
-->
|