mongodb - Spring Data Mongo ExampleMatcher 通过 AND 和 OR 组合获取可分页结果
问题描述
具有以下字段的用户的 MongoDB 集合。
@Data
@Document(collection = "users")
public class UserDoc {
@Id
private String id;
private String firstName;
private String lastName;
private String email;
private UserType type;
public void setFilter(String filter) {
this.firstName = filter;
this.lastName = filter;
this.email = filter;
}
public enum UserType {
ADMIN, REGULAR_USER
}
}
使用 findAll(Example s, Pageable pageable) repo 方法根据以下条件从该集合中搜索。
var user = new User();
user.setType(type);
user.setFilter(filter);
var example = Example.of(user, ExampleMatcher.matchingAny().withIgnoreNullValues()
.withMatcher("type", exact())
.withMatcher("firstName", contains().ignoreCase())
.withMatcher("lastName", contains().ignoreCase())
.withMatcher("email", contains().ignoreCase()));
试图复制一些类似的东西,
{
"$and": [
{
"type": "REGULAR_USER"
},
{
"$or": [
{
"firstName": {
"$regex": ".*john.*",
"$options": "i"
}
},
{
"lastName": {
"$regex": ".*john.*",
"$options": "i"
}
},
{
"email": {
"$regex": ".*john.*",
"$options": "i"
}
}
]
}
]
}
请提示完成此操作的正确方法。如果我可以完成任何其他方式,请提示。
解决方案
推荐阅读
- python - 收集熊猫数据框中特定列值的所有行
- linux - 在 Postgres docker 容器中启动系统服务
- jquery - 带有 CSS 和 Jquery 的多面翻转卡片
- flutter - 如何在 Flutter 中捕获并报告渲染库捕获的异常?
- python - 烧瓶中的身份验证错误登录名或密码
- php - 如何在不使用 artisan 的情况下在远程 ubuntu 服务器上连接 Laravel 7.12 项目的数据库
- r - 根据来自另一个数据框的值向数据框添加一列
- api - Flutter api 重新加载每个导航
- ios - Xcode 测试覆盖率多次显示相同的目标 - 一个总是空的
- mysql - 为什么我不能将自动增量设置为在另一个表中用作外键的字段?