簡體   English   中英

使用spring-data Mongodb無法獲得預期的輸出

[英]Unable to get Expected output using spring-data Mongodb

問題

我必須按名稱搜索用戶,聯系任何用戶類型,即,如果他鍵入名稱,則他僅獲得名稱;如果他鍵入聯系人,則僅獲得聯系人,等等, 而是獲得所有用戶結果。

解決方案-

第一種方法

我已經使用彈簧數據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);

}

輸入提供

{
"searchKey": "98111648642"
}

預期產量

 {
  "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"
 }
 ]
 }

下面我只顯示2個輸出

顯示的輸出

{
  "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)我可以檢查數據是來自用戶設置的還是設置隨機數據的,盡管我尚未實現它,但我正在尋找替代方法。

誰能指導我做錯了什么或任何其他方法?

在您的情況下,findByFirstNameOrContactOrLocationOrGenderInOrProfessionInOrInterestsInOrHeightOrAgeBetween有效意味着

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

由於您使用的是OR,因此它將獲取所有記錄。 因為可能有些屬性對於所有記錄都為空。
正如@Niel所建議的,擁有這么長的查詢語句不是一個好主意。 spring-data-mongodb可以解決這個問題,但是您會變得不清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM