Missing Index Warning
On this page
Definition
The MongoDB for IntelliJ Plugin examines if application queries use indexes. If a query doesn't use an index, the plugin displays a warning for that query.
To resolve the warning, consider creating an index for the query.
Before you add an index, consider if:
The query runs often enough to justify reducing the write performance for faster reads.
You can change the query to use an existing index.
You can also disable index warnings.
For more information about indexes, see Create an Index.
Examples
In the following example Java code snippet, the awards
document
field is used in a query, but the field isn't indexed in the database:
client.getDatabase( "sample_mflix" ).getCollection( "movies" ).find( Filters.ne( "awards", "Comedy" ) )
The plugin shows this warning:
This query will run without an index. If you plan on using this query heavily in your application, you should create an index that covers this query. Implement an Index
Create an Index
To create an index for the query, click the Implement an Index link displayed with the warning in the plugin.
The plugin then displays the Database Explorer Playgrounds
screen with template code for creating an index. The template code also
includes a comment that shows potential fields to index. For example,
the first line of the following code indicates the awards
field
could be indexed:
// Potential fields to consider indexing: awards // Learn about creating an index: https://mongodb.prakticum-team.ru/docs/v7.0/core/data-model-operations/#indexes db.getSiblingDB("sample_mflix").getCollection("movies"). createIndex({"<your_field_1>": 1})
To create an index for the awards
field, set <your_field_1>
to awards
in the example code and then run the
createIndex()
method in the Database
Explorer Playgrounds screen. For example:
db.getSiblingDB("sample_database").getCollection("movies"). createIndex({"awards": 1})
Disable Index Warning
To disable the index warning in the plugin:
Open the IntelliJ IDEA system menu and click Settings.
Expand Editor.
Click Inspections.
Expand MongoDB.
Expand Probable bugs.
Disable Query does not use an index.