gpt4 book ai didi

Spring Data MongoDB 存储库查询多个字段

转载 作者:行者123 更新时间:2023-12-02 13:59:31 26 4
gpt4 key购买 nike

这是我的文档:

@Document(collection = "posts")
public class Post {
@Id
String id;
String title;
String description;
}

我正在尝试使用类似运算符查询标题或描述的帖子。我的存储库

public class PostsRepository extends MongoRepository<Post, String> {
@Query(value = "{ $or: [ { 'title' : ?0 }, { 'description' : ?0 } ] }")
Page<Post> queryByTitleOrDescription(String query, Pageable pageable);
}

如何在两个字段之间使用 LIKE 执行此查询?

最佳答案

如果需要,您甚至可以不使用任何显式查询声明:

interface PostRepository extends Repository<Post, String> {

// A default query method needs parameters per criteria

Page<Post> findByTitleLikeOrDescriptionLike(String title,
String description,
Pageable pageable);

// With a Java 8 default method you can lipstick the parameter binding
// to accept a single parameter only

default Page<Post> findByTitleOrDescription(String candidate, Pageable pageable) {
return findByTitleLikeOrDescriptionLike(candidate, candidate, pageable);
}
}

关于Spring Data MongoDB 存储库查询多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33056062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com