MongoDB for IntelliJ 插件目前处于公共预览阶段。
定义
MongoDB for IntelliJ 插件会检查应用程序查询是否使用索引。如果查询不使用索引或仅部分被索引覆盖,则该插件会显示该查询的警告。
要解决此警告,请考虑为查询创建索引。
在添加索引之前,请考虑以下情况:
查询运行的频率足够高,因此有必要降低写入性能以加快读取速度。
您可以更改查询以使用现有索引。
您还可以禁用索引警告。
有关索引的更多信息,请参阅 索引。
示例
在以下示例Java代码片段中,在查询中使用了 awards
文档字段,但该字段未在数据库中建立索引:
client.getDatabase( "sample_mflix" ).getCollection( "movies" ).find( Filters.ne( "awards", "Comedy" ) )
侧面板在 Performance Warnings 下显示以下警告:

创建索引
要为查询创建索引,请执行以下操作:
1
单击侧面板警告中显示的“创建索引”按钮。
该插件显示 Database Explorer Playgrounds 屏幕,其中包含用于创建索引的模板代码:
// region Queries covered by this index // alt.mongodb.javadriver.JavaDriverRepository#getRatings at line 32 // endregion // Learn about creating an index: https://www.mongodb.com/zh-cn/docs/v7.0/core/data-model-operations/#indexes db.getSiblingDB("sample_mflix").getCollection("movies") .createIndex({ "awards": 1 })
2
在奖项字段上创建索引。
在示例代码中将 <your_field_1>
设置为 awards
,然后在 Database Explorer Playgrounds 屏幕中运行createIndex()
方法。示例:
db.getSiblingDB("sample_database").getCollection("movies"). createIndex({"awards": 1})