简体   繁体   English

使用spring-data Mongodb无法获得预期的输出

[英]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. 我已经使用弹簧数据MongoDb应用或查询了findByNameOrContactOrLocation等,下面是我的代码。

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 下面我只显示2个输出

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. 1)我可以检查数据是来自用户设置的还是设置随机数据的,尽管我尚未实现它,但我正在寻找替代方法。

Can anyone guide for what am I doing wrong or any alternate approach? 谁能指导我做错了什么或任何其他方法?

findByFirstNameOrContactOrLocationOrGenderInOrProfessionInOrInterestsInOrHeightOrAgeBetween in your case effectively means 在您的情况下,findByFirstNameOrContactOrLocationOrGenderInOrProfessionInOrInterestsInOrHeightOrAgeBetween有效意味着

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. 由于您使用的是OR,因此它将获取所有记录。 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. 正如@Niel所建议的,拥有这么长的查询语句不是一个好主意。 spring-data-mongodb can handle this, but you will lose clarity. spring-data-mongodb可以解决这个问题,但是您会变得不清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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