svn: /phpdoc/en/trunk/reference/mongo/ queries.xml
| From: | Kristina Chodorow | Date: | Tue, 11 May 2010 14:49:17 +0000 |
| Subject: | svn: /phpdoc/en/trunk/reference/mongo/ queries.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-6630@lists.php.net to get a copy of this message | ||
kristina Tue, 11 May 2010 14:49:17 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=299248
Log:
added $in section
Changed paths:
U phpdoc/en/trunk/reference/mongo/queries.xml
Modified: phpdoc/en/trunk/reference/mongo/queries.xml
===================================================================
--- phpdoc/en/trunk/reference/mongo/queries.xml 2010-05-11 14:49:16 UTC (rev 299247)
+++ phpdoc/en/trunk/reference/mongo/queries.xml 2010-05-11 14:49:17 UTC (rev 299248)
@@ -59,19 +59,46 @@
<title>Arrays</title>
<para>
- Suppose that we wish to find all documents with an array element of a given
- value. For example, documents with a "gold" award, such as:
+ Arrays are special in a couple ways. First, there are two types that
+ MongoDB uses: "normal" arrays and associative arrays. Associative arrays can
+ have any mix of key types and values. "Normal" arrays are defined as arrays
+ with ascending numeric indexes starting at 0 and increasing by one for each
+ element. These are, typically, just your usual PHP array.
+ </para>
- <programlisting>
+ <para>
+ For instance, if you want to save a list of awards in a document, you could
+ say:
+ </para>
+
+ <programlisting role="php">
<![CDATA[
+<?php
+
+$collection->save(array("awards" => array("gold", "silver",
"bronze")));
+
+?>
+]]>
+ </programlisting>
+
+ <para>
+ Queries can reach into arrays to search for elements. Suppose that we wish
+ to find all documents with an array element of a given value. For example,
+ documents with a "gold" award, such as:
+ </para>
+
+ <programlisting>
+<![CDATA[
{ "_id" : ObjectId("4b06c282edb87a281e09dad9"), "awards" :
["gold", "silver", "bronze"]}
]]>
- </programlisting>
-
+ </programlisting>
+
+ <para>
This can be done with a simple query, ignoring the fact that "awards" is an
array:
+ </para>
- <programlisting role="php">
+ <programlisting role="php">
<![CDATA[
<?php
@@ -79,14 +106,14 @@
?>
]]>
- </programlisting>
- </para>
+ </programlisting>
<para>
Suppose we are querying for a more complex object, if each element of the
array were an object itself, such as:
+ </para>
- <programlisting>
+ <programlisting>
<![CDATA[
{
"_id" : ObjectId("4b06c282edb87a281e09dad9"),
@@ -104,27 +131,45 @@
]
}
]]>
- </programlisting>
+ </programlisting>
+ <para>
Still ignoring that this is an array, we can use dot notation to query the
subobject:
+ </para>
- <programlisting role="php">
+ <programlisting role="php">
<![CDATA[
<?php
- $cursor = $collection->find(array("awards.first place" => "gold"));
+$cursor = $collection->find(array("awards.first place" => "gold"));
?>
]]>
- </programlisting>
+ </programlisting>
+ <para>
Notice that it doesn't matter that there is a space in the the field name
(although it may be best not to use spaces, just to make things more
readable).
</para>
+
+ <para>
+ You can also use an array to query for a number of possible values. For
+ instance, if we were looking for documents "gold" or "copper", we could do:
+ </para>
+
+ <programlisting role="php">
+<![CDATA[
+<?php
+
+$cursor = $collection->find(array("awards" => array('$in' =>
array("gold", "copper"))));
+
+?>
+]]>
+ </programlisting>
+
</section>
-
</section>
<!-- Keep this comment at the end of the file