在mongodb中根据某个数组的字段模糊查询包含其所有的数据使用java中mongoTemplate和Query怎么实现
在mongodb数据库中模糊查询语句
//tags是个字符串数组
db.getCollection('hdfDoctors').find({"state":1,"province" : "北京","tags":{$in:[/XXX/,/XXX/]}})
在nodejs中的操作:
//data.tags是一个带有模糊匹配的数组,如[/XXX/, /XXX/]
param.tags = {$in: data.tags};
count = await DoctorDao.count(param);
下面是Doctor.js
count:async function(query){
query.state =1;
let result = await mongoHelper.count(hdfDoctorModel, query);
return result;
},
在java中的写法
String[] split = tags.split(",");
Set set = new HashSet();
//模糊匹配
Pattern pattern = Pattern.compile("^.*XXX.*$", Pattern.CASE_INSENSITIVE);
set.add(pattern);
query.addCriteria(Criteria.where("tags").in(set));