JAVA操作MongoDB进行条件查询
程序员文章站
2022-05-23 09:19:52
...
前台传入要条查的值script代码
//条查搜索
function search(){
var comments = $("#comments").val();
var sratrcreatetime = $("#sratrcreatetime").datebox("getValue");
var endcreatetime = $("#endcreatetime").datebox("getValue");
$("#pinglunTable").datagrid("load",{
comments:comments,
sratrcreatetime:sratrcreatetime,
endcreatetime:endcreatetime
})
}
Service层new一个Query对象进行条查
Query query = new Query();
精确查询
//根据id精确查询
query.addCriteria(Criteria.where("goodid").is(goodid));
模糊查询
//模糊搜索
if (StringUtils.isNotEmpty(pinglunBean.getComments())) {
query.addCriteria(Criteria.where("comments").regex(pinglunBean.getComments()));
}
区间查询
//区间搜索
if(pinglunBean.getSratrcreatetime() != null && pinglunBean.getEndcreatetime() != null) {
query.addCriteria(Criteria.where("createtime").gte(pinglunBean.getSratrcreatetime()).lte(pinglunBean.getEndcreatetime()));
}