Himanshu Kawale
Himanshu Kawale

Reputation: 364

Spring boot MongoRepository query to match default query if main query does not match

I've a collection in which I have multiple records, as below,

Name age
Abcd 100
pqrs 200
none 000

User will give the name, and my method will fetch data and return age, fetching based on "Name", as below,

res = mongoRepository.findByName(userName);

but I want to fetch data for Name "none" if the name provided by user is not present in collection. So currently I'm doing as below,

res = mongoRepository.findByName(userName);

if (res1 == null){
   res = mongoRepository.findByName("none");
}

But is there any way to do this in single call ?

Please note, I can not hardcode data for "none" in code, I must fetch it from DB.

Thanks for any help.

Upvotes: 0

Views: 67

Answers (0)

Related Questions