2010-08-26 16:21:38 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<!-- $Revision$ -->
|
|
|
|
|
2012-11-28 03:03:34 +00:00
|
|
|
<chapter xml:id="mongo.writes" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
2010-08-26 16:21:38 +00:00
|
|
|
<title>Writes</title>
|
|
|
|
<section>
|
|
|
|
<title>Updating Nested Objects</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Suppose we wish to change the name of a comment's author in this document:
|
|
|
|
|
2014-09-09 18:31:20 +00:00
|
|
|
<programlisting role="javascript">
|
2010-08-26 16:21:38 +00:00
|
|
|
<![CDATA[
|
|
|
|
{
|
|
|
|
"_id" : ObjectId("4b06c282edb87a281e09dad9"),
|
|
|
|
"content" : "this is a blog post.",
|
|
|
|
"comments" :
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"author" : "Mike",
|
|
|
|
"comment" : "I think that blah blah blah...",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"author" : "John",
|
|
|
|
"comment" : "I disagree."
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
In order to change an inner field, we use $set (so that all of the other
|
|
|
|
fields are not removed!) with the index of comment to change:
|
|
|
|
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$blog->update($criteria, array('$set' => array("comments.1" => array("author" => "Jim"))));
|
|
|
|
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>The Positional Operator</title>
|
|
|
|
<para>
|
|
|
|
The positional operator <literal>$</literal> is useful for updating objects
|
|
|
|
that are in arrays. In the example above, for instance, suppose that we did
|
|
|
|
not know the index of the comment that we needed to change, merely that we
|
|
|
|
needed to change "John" to "Jim". We can use <literal>$</literal> to do so.
|
|
|
|
</para>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$blog->update(
|
2013-12-10 11:38:03 +00:00
|
|
|
array('comments.author' => 'John'),
|
|
|
|
array('$set' => array('comments.$.author' => 'Jim')));
|
2010-08-26 16:21:38 +00:00
|
|
|
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</section>
|
2012-11-28 08:27:11 +00:00
|
|
|
|
2012-11-28 03:03:34 +00:00
|
|
|
</chapter>
|
2010-08-26 16:21:38 +00:00
|
|
|
|
|
|
|
<!-- 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
|
|
|
|
-->
|
|
|
|
|