关于Spring MongoDB的多字段模糊查询

模糊查询的action

@RequestMapping(value = "/fuzzySelect", method = RequestMethod.GET)
    @ResponseBody
    public ResultData fuzzySelect(@RequestParam("name") String name) {
        ResultData resultData = new ResultData();
            String a = "^.*";
            String b = ".*$";
            String c = a + name + b;
            List<Data> sites = dao.fuzzyselect(c);
            resultData.setStatus(Constant.SUCCESS);
            resultData.setMessage("成功");
            resultData.setData(sites);
            return resultData;
    }
@Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Data> fuzzyselect(String c) {
        Query query = Query.query(
                Criteria.where("")
                .orOperator(
                        Criteria.where("属性1").regex(c),
                        Criteria.where("属性2").regex(c),
                        Criteria.where("属性3").regex(c)));
        List<Data> datas=mongoTemplate.find(query, Data.class);
        return datas;
    }

你可能感兴趣的:(Spring,MongoDB)