在spring数据mongodb中过滤记录时没有输出

时间:2017-12-31 09:46:14

标签: java spring-boot spring-data-mongodb

问题

我正在尝试根据用户的品牌,车辆模式等检索记录,但我没有收到任何记录。

我做的任务

我使用mongoTemplate来检索记录。我正在应用条件,如果该字段不为null,我将检索,否则我将无法检索。

以下是我的代码

车辆型号

@Document
public class Vehicle extends AbstractEntity {
@Id
private String id;
private String yearOfBuying;
private String brand;
private String model;
private String mileage;
private String availableFuel;
private String vehicleRcNo;
private String vehicleNumber;
private String rcImageId;
private String description;
private String vehicleName;
private VEHICLE_TYPE vehicleType;
private VEHICLE_MODE vehicleMode;
private FUEL_TYPE fuelType;
private List<WALLET_TYPE> walletTypeList;
private List<AVAILABLE_WITH> availableWithList;
private List<PRICE_TYPE> priceTypeList;
private boolean availability = true;
private String sellAddress;
private String city;
private String state;
private String pinCode;
private String userId;
@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)
private double[] location;
private float securityAmount;
private long availableFrom;
private long availableTo;
private String aboutVehicle;
private float discountPercentage;
private String dos;
private String dont;
private long pollutionDate;
private String pollutionImageId;
private long endInsuranceDate;
private String insuranceImageId;
private float pricePerDay;
private float pricePerDayWithDriver;
private float pricePerHour;
private float pricePerHourWithDriver;
private float pricePerWeek;
private float pricePerWeekWithDriver;
private float pricePerMonth;
private float pricePerMonthWithDriver;

VehicleController

@GetMapping(value = "/searchByBrand")
ApiResponse<List<Vehicle>> searchRecords() {
    return ApiResponse.success().object(vehicleService.searchByBrandOrVehicleTypeOrFuelType("Mahindra","BIKE","DIESEL"));
}

界面VehicleService

List<Vehicle> searchByBrandOrVehicleTypeOrFuelType(String brand,String vehicleType,String fuelType);

VehicleServiceImpl

@Override
public List<Vehicle> searchByBrandOrVehicleTypeOrFuelType(String brand,String vehicleType,String fuelType) {
    return vehicleRepository.searchByBrandOrVehicleTypeOrFuelType(brand,vehicleType,fuelType);
}

VehicleRepository

public interface VehicleRepository extends MongoRepository<Vehicle, String>,VehicleRepositoryCustom {

VehicleRepositoryCustom

public interface VehicleRepositoryCustom {
//Advance query
List<Vehicle> searchByBrandOrVehicleTypeOrFuelType(String searchPattern,String vehicleType,String fuelType);

}

VehicleRepositoryImpl

public class VehicleRepositoryImpl implements VehicleRepositoryCustom{
@Autowired
private MongoTemplate mongoTemplate;
private Class<Vehicle> clazz = Vehicle.class;

@Override
public List<Vehicle> searchByBrandOrVehicleTypeOrFuelType(String brand,String vehicleType,String fuelType) {
    Query query = new Query();
    if(brand!=null){
        System.out.println("Executed Not null");
        query.addCriteria(Criteria.where("brand").is(brand));
    }else {
        query.addCriteria(Criteria.where("brand").exists(false));
    }
    if(vehicleType!=null){
        System.out.println("Executed Not null");
        query.addCriteria(Criteria.where("vehicleType").is(vehicleType));
    }else {
        query.addCriteria(Criteria.where("vehicleType").exists(false));
    }
    if(fuelType!=null){
        System.out.println("Executed Not null");
        query.addCriteria(Criteria.where("fuelType").is(fuelType));
    }else {
        query.addCriteria(Criteria.where("fuelType").exists(false));
    }
    return mongoTemplate.find(query, clazz);
}

预期输出

匹配条件的记录

显示输出

[]

  

任何人都可以指导我如何正确应用查询吗?

0 个答案:

没有答案