@@ -6,10 +6,106 @@ Missing Index Warning
6
6
7
7
.. default-domain:: mongodb
8
8
9
+ .. meta::
10
+ :description: The MongoDB for IntelliJ Plugin examines Java code for missing indexes.
11
+
9
12
.. contents:: On this page
10
13
:local:
11
14
:backlinks: none
12
15
:depth: 1
13
16
:class: singlecol
14
17
15
- Content on this page will be addressed in DOCSP-44966.
18
+ Definition
19
+ ----------
20
+
21
+ The {+intellij-full+} examines if application queries use indexes. If a
22
+ query doesn't use an index, the plugin displays a warning for that
23
+ query.
24
+
25
+ To resolve the warning, consider creating an index for the query.
26
+
27
+ Before you add an index, consider if:
28
+
29
+ - The query runs often enough to justify reducing the write performance
30
+ for faster reads.
31
+ - You can change the query to use an existing index.
32
+
33
+ You can also disable index warnings.
34
+
35
+ For more information about indexes, see :ref:`manual-create-an-index`.
36
+
37
+ Examples
38
+ --------
39
+
40
+ In the following example Java code snippet, the ``awards`` document
41
+ field is used in a query, but the field isn't indexed in the database:
42
+
43
+ .. code-block:: java
44
+ :copyable: false
45
+ :emphasize-lines: 2
46
+
47
+ client.getDatabase( "sample_mflix" ).getCollection( "movies" ).find(
48
+ Filters.ne( "awards", "Comedy" )
49
+ )
50
+
51
+ The plugin shows this warning:
52
+
53
+ .. code-block:: none
54
+ :copyable: false
55
+
56
+ This query will run without an index. If you plan on using this
57
+ query heavily in your application, you should create an index that
58
+ covers this query.
59
+
60
+ Implement an Index
61
+
62
+ Create an Index
63
+ ~~~~~~~~~~~~~~~
64
+
65
+ To create an index for the query, click the :guilabel:`Implement an
66
+ Index` link displayed with the warning in the plugin.
67
+
68
+ The plugin then displays the :guilabel:`Database Explorer Playgrounds`
69
+ screen with template code for creating an index. The template code also
70
+ includes a comment that shows potential fields to index. For example,
71
+ the first line of the following code indicates the ``awards`` field
72
+ could be indexed:
73
+
74
+ .. code-block:: java
75
+ :copyable: false
76
+ :emphasize-lines: 1
77
+
78
+ // Potential fields to consider indexing: awards
79
+ // Learn about creating an index: https://www.mongodb.com/docs/v7.0/core/data-model-operations/#indexes
80
+ db.getSiblingDB("sample_mflix").getCollection("movies").
81
+ createIndex({"<your_field_1>": 1})
82
+
83
+ To create an index for the ``awards`` field, set ``<your_field_1>``
84
+ to ``awards`` in the example code and then run the
85
+ :method:`~db.collection.createIndex()` method in the :guilabel:`Database
86
+ Explorer Playgrounds` screen. For example:
87
+
88
+ .. code-block:: java
89
+ :emphasize-lines: 2
90
+
91
+ db.getSiblingDB("sample_database").getCollection("movies").
92
+ createIndex({"awards": 1})
93
+
94
+ Disable Index Warning
95
+ ~~~~~~~~~~~~~~~~~~~~~
96
+
97
+ To disable the index warning in the plugin:
98
+
99
+ 1. Open the IntelliJ IDEA system menu and click :guilabel:`Settings`.
100
+ #. Expand :guilabel:`Editor`.
101
+ #. Click :guilabel:`Inspections`.
102
+ #. Expand :guilabel:`MongoDB`.
103
+ #. Expand :guilabel:`Probable bugs`.
104
+ #. Disable :guilabel:`Query does not use an index`.
105
+
106
+ Learn More
107
+ ----------
108
+
109
+ - :ref:`intellij-connect`
110
+ - :ref:`intellij-autocomplete`
111
+ - :ref:`intellij-type-validation`
0 commit comments