50074

Question:
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))
)
)
)
);
Answer1: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)
)
)