简体   繁体   中英

Unable to get Expected output using spring-data Mongodb

Problem

I have to search user by name,contact whatever user types ie if he types name he gets name only and if he type contact he gets contact only etc instead I am getting all user results.

Solution -

1st Approach

I have applied or query ie findByNameOrContactOrLocation etc using spring data MongoDb.Below is my code.

public class Search {
private String searchKey;
private double[] location;
private List<Gender> gender;
private List<String> profession;
private List<INTERESTS> interests;
private int minAge;
private int maxAge;
private double[] height;



 @PutMapping(value = "/searchByEndUser")
ApiResponse<List<Employee>> searchEndUserBasedOnKeyWord(@RequestBody Search params) {
    return ApiResponse.success().object(employeeService.getByWord(params));
}


@Service
public class EmployeeServiceImpl implements EmployeeService {

@Override
public List<Employee> getByWord(Search params) {
    return employeeRepository.findByFirstNameOrContactOrLocationOrGenderInOrProfessionInOrInterestsInOrHeightOrAgeBetween(params.getSearchKey(), params.getSearchKey(), params.getLocation(), params.getGender(), params.getProfession(), params.getInterests(), params.getHeight(), params.getMinAge(), params.getMaxAge());
}

public interface EmployeeRepository extends MongoRepository<Employee, String> {
List<Employee> findByFirstNameOrContactOrLocationOrGenderInOrProfessionInOrInterestsInOrHeightOrAgeBetween(String searchKey, String searchKey1, double[] location, List<Gender> gender, List<String> profession, List<INTERESTS> interests, double[] height, int minAge, int maxAge);

}

Input Supplied

{
"searchKey": "98111648642"
}

Output Expected

 {
  "createdBy": "anonymousUser",
  "lastModifiedBy": "anonymousUser",
  "version": 0,
  "createdAt": 1510303944245,
  "lastModifiedAt": 1510303944245,
  "imageIds": null,
  "concerns": null,
  "summary": null,
  "likings": null,
  "occupation": null,
  "religion": null,
  "education": null,
  "height": null,
  "location": null,
  "interests": null,
  "fcmId": null,
  "connections": null,
  "declined": null,
  "pending": null,
  "likes": 0,
  "dislikes": 0,
  "review": null,
  "ratings": null,
  "favourites": null,
  "disConnReason": null,
  "employee_status": null,
  "employee_role": null,
  "age": 0,
  "notification": false,
  "contact": "98111648642",
  "email": null,
  "address": "13",
  "gender": null,
  "imageId": null,
  "firstName": null,
  "lastName": null,
  "middleName": null,
  "dob": null,
  "username": "jack",
  "country": null,
  "designation": null,
  "aboutMe": null,
  "profession": null,
  "sos": null,
  "secret": null,
  "socialProfile": null,
  "socialProfileLink": null,
  "socialToken": null,
  "socialProfilePicture": null,
  "referralCode": null,
  "refCons": 0,
  "referredUserId": null,
  "choice": null,
  "id": "5a0568c879eb192b872592b3"
 }
 ]
 }

Below I am showing only 2 outputs

Output Shown

{
  "createdBy": "anonymousUser",
  "lastModifiedBy": "anonymousUser",
  "version": 0,
  "createdAt": 1510228467303,
  "lastModifiedAt": 1510228467303,
  "imageIds": null,
  "concerns": null,
  "summary": null,
  "likings": null,
  "occupation": null,
  "religion": null,
  "education": null,
  "height": null,
  "location": null,
  "interests": null,
  "fcmId": null,
  "connections": null,
  "declined": null,
  "pending": null,
  "likes": 0,
  "dislikes": 0,
  "review": null,
  "ratings": null,
  "favourites": null,
  "disConnReason": null,
  "employee_status": null,
  "employee_role": null,
  "age": 0,
  "notification": false,
  "contact": "9811164864",
  "email": null,
  "address": "32",
  "gender": null,
  "imageId": null,
  "firstName": null,
  "lastName": null,
  "middleName": null,
  "dob": null,
  "username": "jui",
  "country": null,
  "designation": null,
  "aboutMe": null,
  "profession": null,
  "sos": null,
  "secret": null,
  "socialProfile": null,
  "socialProfileLink": null,
  "socialToken": null,
  "socialProfilePicture": null,
  "referralCode": null,
  "refCons": 0,
  "referredUserId": null,
  "choice": null,
  "id": "5a0441f379eb1930d9f961b2"
},
{
  "createdBy": "anonymousUser",
  "lastModifiedBy": "anonymousUser",
  "version": 0,
  "createdAt": 1510303944245,
  "lastModifiedAt": 1510303944245,
  "imageIds": null,
  "concerns": null,
  "summary": null,
  "likings": null,
  "occupation": null,
  "religion": null,
  "education": null,
  "height": null,
  "location": null,
  "interests": null,
  "fcmId": null,
  "connections": null,
  "declined": null,
  "pending": null,
  "likes": 0,
  "dislikes": 0,
  "review": null,
  "ratings": null,
  "favourites": null,
  "disConnReason": null,
  "employee_status": null,
  "employee_role": null,
  "age": 0,
  "notification": false,
  "contact": "98111648642",
  "email": null,
  "address": "13",
  "gender": null,
  "imageId": null,
  "firstName": null,
  "lastName": null,
  "middleName": null,
  "dob": null,
  "username": "jack",
  "country": null,
  "designation": null,
  "aboutMe": null,
  "profession": null,
  "sos": null,
  "secret": null,
  "socialProfile": null,
  "socialProfileLink": null,
  "socialToken": null,
  "socialProfilePicture": null,
  "referralCode": null,
  "refCons": 0,
  "referredUserId": null,
  "choice": null,
  "id": "5a0568c879eb192b872592b3"
}
]
}

**2nd Approach**

1)I can check if the data is coming from user set it or set a random data.Though I have not implemented it.I am looking for alternate approach for this.

Can anyone guide for what am I doing wrong or any alternate approach?

findByFirstNameOrContactOrLocationOrGenderInOrProfessionInOrInterestsInOrHeightOrAgeBetween in your case effectively means

Get_me_documents_where {
    firstname : "98111648642"
      or
     contact : "98111648642"
      or
    location : null
      or 
    gender : null
      or
   ......
}

Since you are using OR, it will fetch all the records. Because there might be some property which is null for all the records.
As @Niel suggested, it is not a good idea to have such a long query statement. spring-data-mongodb can handle this, but you will lose clarity.

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