简体   繁体   中英

OrOperator in MongoTemplate not returning values?

I am working on Spring MVC with MongoDB. I have added criteria for some fields with orOperator, Each Fields has value in database, but List returning is Empty.

criteriaQuery.addCriteria(Criteria.where("First_Name").is(docName.trim())
.orOperator(Criteria.where("Middle_Name").is(docName.trim())
.orOperator(Criteria.where("Last_Name").is(docName.trim()))));

Query printing in console is

 Query: { "First_Name" : "ESTHER" , "$or" : [ { "Middle_Name" : "ESTHER" , "$or" : [ { "Last_Name" : "ESTHER"}]}]}, Fields: null, Sort: null

I tried the Like query, but that also results the same

 criteriaQuery.addCriteria(Criteria.where("First_Name").regex(docName)
.orOperator(Criteria.where("Middle_Name").regex(docName))
    .orOperator(Criteria.where("Last_Name").regex(docName)));

Mongo Query

db.doctor_details.find({  "$or" : {"First_Name" : { "$regex" : "ESTHER"} }, "$or" : [ { "Middle_Name" : { "$regex" : "ESTHER"} , "$or" : [ { "Last_Name" : { "$regex" : "ESTHER"}}]}]})

Help me to get the result, I dont know where my code goes wrong. Help is appreciated!

Thanks

As far as I remember in order to use orOperator you should do:

Query query = new Query(new Criteria().orOperator(criteriaV1,criteriaV2));

it is based on this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM