0

具有以下字段的用户的 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"
          }
        }
      ]
    }
  ]
}

请提示完成此操作的正确方法。如果我可以完成任何其他方式,请提示。

4

0 回答 0