MongoDB for IntelliJ 插件目前处于公共预览阶段。
MongoDB for IntelliJ 插件会验证Java驾驶员或 Spring Criteria 代码中的数据库引用,以确保指定的数据库、集合或字段在服务器中存在。
如果引用的字段、集合或数据库名称不在数据源中,则该插件会显示警告,指示该引用不存在。
要解决该警告,请执行以下操作:
确保您已连接到连接工具栏中的正确数据源。
检查是否在代码中引用了正确的数据库和集合。
验证您的数据库或集合是否包含您尝试引用的字段。
不存在的字段名称
如果引用集合中不存在的字段名称,IntelliJ 插件会在侧面板的 Correctness Warnings 下引发以下警告:
Field <fieldName> does not seem to exist in collection.
不存在的集合名称
如果引用数据库中不存在的集合名称,IntelliJ 插件会在侧面板的 Environment Mismatch Warnings 下引发以下警告:
Cannot resolve <collectionName> collection in <dbName> database in the connected data source.
要解决此警告,请确保数据库中存在引用的集合。或者,您可以单击侧面板中的 Choose a connection,选择包含引用集合的其他数据源。
Non-existent Database Name
如果引用数据源中不存在的数据库,IntelliJ 插件会在侧面板的 Environment Mismatch Warnings 下引发以下警告:
Cannot resolve <dbName> database reference in the connected data source.
要解决此警告,请确保引用的数据库存在于您的部署中。或者,您可以单击侧面板中的 Choose a connection,选择包含引用数据库的其他数据源。
例子
以下示例引用了sample_mflix
数据库,其中包含 Atlas示例数据集中的电影和电影院的相关数据。
示例代码尝试调用 restaurant_name
集合:
public List<Document> getHundredYearOldMovies() { return client.getDatabase("sample_mflix") .getCollection("restaurant_name") .find(Filters.eq("year", 1924)) .into(new ArrayList<>()); }
由于 sample_mflix
数据库中不存在该集合,因此 IntelliJ 插件会发出无法解析该集合的警告:
Cannot resolve "restaurant_name" collection in "sample_mflix" database in the connected data source.``
要解决此警告,请引用 sample_mflix
数据库中存在的集合:
public List<Document> getHundredYearOldMovies() { return client.getDatabase("sample_mflix") .getCollection("movies") .find(Filters.eq("year", 1924)) .into(new ArrayList<>()); }