首页 > 解决方案 > Spring JPA Query 查找属于查询字符串的所有字符串

问题描述

我想用一个例子来解释我的问题。在 mongodb 我有一个字符串列。现在我想列出该列中属于我的查询字符串的所有字符串。

例如,如果我的查询字符串是“这是我的测试字符串”。

我的理想结果是

“这个”、“这是”、“这是我的”、“是我的”、“是我的测试”、“这是我的测试”、“这是我的测试字符串”等..,

在 mysql 中,我知道 "%Like%" 会处理。我在 mongo JPA 中尝试了“Like”,但没有运气。

谁能帮我?

标签: mongodbspring-dataspring-data-mongodb

解决方案


As JB mentioned, there is no Mongo JPA, but I assume you want a similar behavior as what Spring Data JPA does for you which is what Spring Data MongoDB does.

To do so, you need to use In and a collection of the string part of your query. So you will need to break your string first.

In your repository, use Spring Data's query DSL with In i.e:

public interface YourDocumentRepository extends MongoRepository<YourDocument, ClassTypeOfYourId> {

  public List<YourDocument> findByNameIn(string[] text);

}

If the name of your string field is superDuper, the example would be:

public List<YourDocument> findBySuperDuperIn(string[] text);

You should adjust. For more detail, take a look at the reference or some example here.


推荐阅读