SpringData regex not working as expected

柔情痞子 提交于 2019-12-13 03:46:21

问题


Working native query

{
  $match: {
    $and : [
      {userType:"200"},
      {
        $or: [
          {login     : /infosys/},
          {email     : /infosys/},
          {firstName : /infosys/},
          {lastName  : /infosys/}
        ]
      }
    ]
  }
}

SpringData API which is not working as expected:

match(
    Criteria.where("userType").is(userType).orOperator(
        Criteria.where("login").regex(searchTxt).orOperator(
            Criteria.where("email").regex(searchTxt).orOperator(
                Criteria.where("firstName").regex(searchTxt).orOperator(Criteria.where("lastName").regex(searchTxt))
            )
        )
    )
);

回答1:


You are $or each criteria with the $or operator. orOperator takes a list of crtieria.

Below is the equivalent for your native query.

match
(
  Criteria.where("userType").is(userType)
  .orOperator(
      Criteria.where("login").regex(searchTxt), 
      Criteria.where("email").regex(searchTxt), 
      Criteria.where("firstName").regex(searchTxt), 
      Criteria.where("lastName").regex(searchTxt)
    )
)


来源:https://stackoverflow.com/questions/43956516/springdata-regex-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!