@@ -32,13 +32,17 @@ query that specifies multiple predicates on the array and can use a
32
32
:ref:`multikey index <index-type-multikey>` bounds if an
33
33
:query:`$elemMatch` joins the predicates.
34
34
35
- For example, a collection ``survey`` contains documents with a field
36
- ``item`` and an array field ``ratings``:
35
+ For example, create a ``survey`` collection that contains documents
36
+ with a field ``item`` and an array field ``ratings``:
37
37
38
38
.. code-block:: javascript
39
39
40
- { _id: 1, item: "ABC", ratings: [ 2, 9 ] }
41
- { _id: 2, item: "XYZ", ratings: [ 4, 3 ] }
40
+ db.survey.insertMany(
41
+ [
42
+ { _id: 1, item: "ABC", ratings: [ 2, 9 ] },
43
+ { _id: 2, item: "XYZ", ratings: [ 4, 3 ] }
44
+ ]
45
+ )
42
46
43
47
Create a :ref:`multikey index <index-type-multikey>` on the ``ratings``
44
48
array:
@@ -106,13 +110,18 @@ Compound Index on an Array Field
106
110
107
111
Consider a compound multikey index; i.e. a :ref:`compound index
108
112
<index-type-compound>` where one of the indexed fields is an array. For
109
- example, a collection ``survey`` contains documents with a field
113
+ example, create a ``survey`` collection that contains documents with a field
110
114
``item`` and an array field ``ratings``:
111
115
112
116
.. code-block:: javascript
113
117
114
- { _id: 1, item: "ABC", ratings: [ 2, 9 ] }
115
- { _id: 2, item: "XYZ", ratings: [ 4, 3 ] }
118
+ db.survey.insertMany(
119
+ [
120
+ { _id: 1, item: "ABC", ratings: [ 2, 9 ] },
121
+ { _id: 2, item: "XYZ", ratings: [ 4, 3 ] }
122
+ ]
123
+ )
124
+
116
125
117
126
Create a :ref:`compound index <index-type-compound>` on the ``item``
118
127
field and the ``ratings`` field:
@@ -181,12 +190,17 @@ and ratings to ``[[3.0, 6.0]]`` to use the combined bounds of:
181
190
"item" : [ [ "L", "Z" ] ], "ratings" : [ [3.0, 6.0] ]
182
191
183
192
For another example, consider where the scalar fields belong to a nested document.
184
- For instance, a collection ``survey`` contains the following documents:
193
+ For instance, create a ``survey`` collection that contains the following documents:
185
194
186
195
.. code-block:: javascript
187
-
188
- { _id: 1, item: { name: "ABC", manufactured: 2016 }, ratings: [ 2, 9 ] }
189
- { _id: 2, item: { name: "XYZ", manufactured: 2013 }, ratings: [ 4, 3 ] }
196
+
197
+ db.survey.insertMany(
198
+ [
199
+ { _id: 1, item: { name: "ABC", manufactured: 2016 }, ratings: [ 2, 9 ] },
200
+ { _id: 2, item: { name: "XYZ", manufactured: 2013 }, ratings: [ 4, 3 ] }
201
+ ]
202
+ )
203
+
190
204
191
205
Create a compound multikey index on the scalar fields ``"item.name"``,
192
206
``"item.manufactured"``, and the array field ``ratings`` :
0 commit comments