Skip to content

Commit 174b8c7

Browse files
authored
DOCSP-14352: fix multikey index description & example (#542)
* DOCSP-14352: fix multikey index description & example * add proj field * spacing fix * CC suggestions and adjusting projection so all queries are covered
1 parent ca98e7a commit 174b8c7

File tree

5 files changed

+18
-20
lines changed

5 files changed

+18
-20
lines changed

source/code-snippets/indexes/compound.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function run() {
2222
// begin-query
2323
const query = { type: "movie", genre: "Drama" };
2424
const sort = { type: 1, genre: 1 };
25-
const projection = { type: 1, genre: 1 };
25+
const projection = { _id: 0, type: 1, genre: 1 };
2626

2727
const cursor = movies
2828
.find(query)

source/code-snippets/indexes/multikey.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,21 @@ async function run() {
1313
const database = client.db("sample_mflix");
1414
const movies = database.collection("movies");
1515

16-
// Create a multikey index on the "cast" array field
17-
// in the "movies" collection.
16+
// Create a multikey index on the "cast" field
1817
const result = await movies.createIndex({ cast: 1 });
19-
console.log(`Index created: ${result}`);
2018
// end-idx
2119

20+
console.log(`Index created: ${result}`);
21+
2222
// begin-query
23-
const query = { cast: "Burt Reynolds" };
24-
const sort = { cast: 1, genre: 1 };
25-
const projection = { cast: 1 };
23+
const query = { cast: "Viola Davis" };
24+
const projection = { _id: 0, cast: 1 , title: 1 };
2625

2726
const cursor = movies
2827
.find(query)
29-
.sort(sort)
3028
.project(projection);
3129
// end-query
30+
3231
} finally {
3332
await client.close();
3433
}

source/code-snippets/indexes/single-field.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function run() {
2222
// begin-query
2323
const query = { title: "Batman" }
2424
const sort = { title: 1 };
25-
const projection = { title: 1 };
25+
const projection = { _id: 0, title: 1 };
2626

2727
const cursor = movies
2828
.find(query)

source/code-snippets/indexes/text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function run() {
2121

2222
// begin-query
2323
const query = { $text: { $search: "java coffee shop" } };
24-
const projection = { fullplot: 1 };
24+
const projection = { _id: 0, fullplot: 1 };
2525
const cursor = movies
2626
.find(query)
2727
.project(projection);

source/fundamentals/indexes.txt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,22 @@ To learn more, see :manual:`Compound Indexes </core/index-compound>`.
128128
Multikey Indexes (Indexes on Array Fields)
129129
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130130

131-
**Multikey indexes** are indexes that improve performance for queries that
132-
specify ascending or descending indexes on fields that contain an array value.
133-
You can define a multikey index using the same syntax as a single field or
134-
compound index.
131+
**Multikey indexes** are indexes that improve the performance of queries on
132+
fields that contain array values.
135133

136-
The following example use the ``createIndex()`` method to create an ascending
137-
index on the ``cast`` field (array of names) in the ``movies`` collection in
138-
the ``sample_mflix`` database.
134+
You can create a multikey index on a field with an array value by
135+
calling the ``createIndex()`` method. The following code creates an ascending
136+
index on the ``cast`` field in the ``movies`` collection of the
137+
``sample_mflix`` database:
139138

140139
.. literalinclude:: /code-snippets/indexes/multikey.js
141140
:language: js
142141
:start-after: begin-idx
143142
:end-before: end-idx
144143
:dedent:
145144

146-
The following is an example of a query that would be covered by the index
147-
created above.
145+
The following code queries the multikey index to find
146+
documents with a ``cast`` field value that contains "Viola Davis":
148147

149148
.. literalinclude:: /code-snippets/indexes/multikey.js
150149
:language: js
@@ -156,7 +155,7 @@ Multikey indexes behave differently from non-multikey indexes in terms of
156155
query coverage, index bound computation, and sort behavior. For a full
157156
explanation of multikey indexes, including a discussion of their behavior
158157
and limitations, refer to the :manual:`Multikey Indexes page
159-
</core/index-multikey>` in the MongoDB manual.
158+
</core/index-multikey>` in the MongoDB Server manual.
160159

161160
Clustered Indexes
162161
~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)