SpringData 正则表达式没有按预期工作 [英] SpringData regex not working as expected

查看:56
本文介绍了SpringData 正则表达式没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作原生查询

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

未按预期工作的 SpringData API:

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))
            )
        )
    )
);

推荐答案

使用 $or 运算符是 $or 每个条件.orOperator 获取标准列表.

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)
    )
)

这篇关于SpringData 正则表达式没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆