首页 > 解决方案 > 在 Spingboot 应用程序中,简单的 Mongo DB 查询需要很长时间

问题描述

我有一个使用 MongoDB 作为数据库的 Springboot 应用程序。其中一个集合有一个字符串字段,对该字段的查询需要很长时间,我无法对其进行调整。我已经创建了索引,但仍然没有任何改进。这是我的代码。

public interface MyTableRepository extends MongoRepository<MyTable, String> {
    public List<MyTable> findByState(String state);
}

这是我的索引:

db.getCollection('myTable').createIndex( { "state": 1 } )

当我从我的服务中运行以下代码时,此调用返回大约 800 行并且大约需要 20 秒。

List<MyTable> publishedItems = myRepository.findByState("published");

标签: javamongodbspring-bootspring-mongodb

解决方案


推荐阅读