1

I want to use "Like" like query but result should be scanned from starting e.g., "starts with"

List<Account> findByUserInfo_FirstNameRegexAndUserInfo_LastNameRegexAndEmails_EmailIDRegex(String firstName, String lastName, String emailID);

or

List<Account> findByUserInfo_FirstNameLikeAndUserInfo_LastNameLikeAndEmails_EmailIDLike(String firstName, String lastName, String emailID);

works great with API I made.

Here is my controller code using Spring,

@RequestMapping(value = baseUrl + "/search", method = RequestMethod.GET)
public List<Account> getSimilarAccountList(@RequestParam(value = "fullName", required = false) String fullName, @RequestParam(value = "emailID", required = false) String emailID, @RequestParam(value = "mobile", required = false) String mobileNumber) {
    log.info("Request to fetch all the accounts recieved.");
    return accountService.getSimilarAccountList(fullName, emailID, mobileNumber);

I am calling API through postman using,

http://localhost:9098/account/search?fullName=kk

giving me same result

"firstName": "akki"

but I want it to only works with http://localhost:9098/account/search?fullName=ak

I hope, I could make my question clearer.

  • 1
    You are checking for regex queries. Check out : https://docs.mongodb.org/manual/reference/operator/query/regex/#perform-case-insensitive-regular-expression-match – Sarath Nair Nov 26 '15 at 06:13
  • checked out this doc, but could not find I am looking for. Thanks for link. – Axay Prajapati Nov 26 '15 at 07:11
  • well, I found this link which is helpful to my query: http://stackoverflow.com/questions/3305561/how-to-query-mongodb-with-like/23761146#23761146 but dont know how to implement in my query, because my query is based on supported keywords for query methods found here: http://docs.spring.io/spring-data/mongodb/docs/1.3.3.RELEASE/reference/html/mongo.repositories.html – Axay Prajapati Nov 26 '15 at 07:19

1 Answers1

0

Solved it using:

List<Account> findByUserInfo_FirstNameRegexAndUserInfo_LastNameRegexAndEmails_EmailIDRegex(String firstName, String lastName, String emailID);

and

implementing to the service:

firstName = "^" + firstName;
emailID = "^" + emailID;
lastName = "^" + lastName;    

repository.findByUserInfo_FirstNameRegexAndUserInfo_LastNameRegexAndEmails_EmailIDRegex(firstName, lastName, emailID);

Use Regex with "^" on start of String.