数据库引用验证
MongoDB for IntelliJ 插件目前处于私有预览阶段。
MongoDB for IntelliJ 插件验证 Java 驱动程序或 Spring Criteria 代码中的数据库引用,以确保指定的数据库、集合或字段在服务器中存在。
如果您引用的字段、集合或数据库名称不在您的数据源中,插件会显示一个警告,指出引用不存在。
要解决警告
确保您在“连接工具栏”中连接到正确的数据源。
检查您在代码中引用的是否是正确的数据库和集合。
验证您的数据库或集合中是否包含您要引用的字段。
不存在的字段名称
如果您引用的集合中不存在字段名称,IntelliJ插件将显示以下警告
Field <fieldName> does not exist in collection <collectionName>.
不存在的集合名称
如果您引用的数据库中不存在集合名称,IntelliJ插件将显示以下警告
Cannot resolve <collectionName> collection in <dbName> database in the connected data source.
不存在的数据库名称
如果您引用的数据源中不存在的数据库,IntelliJ插件将显示以下警告
Cannot resolve <dbName> database reference in the connected data source.
示例
以下示例引用了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<>()); }