Mongodb模糊,or,and查询和日期查询

mongodb命令:

模糊:

db.getCollection('cabinTermRecord').find({"personnelName" : /能/}).pretty()

 

正则模糊,or

db.getCollection('cabinTermRecord').find({"$or" : [ { "personnelName" : { "$regex" : ".*0.*"}} , { "termMac" : { "$regex" : "^能.*$"}}]}).pretty()

正则模糊,or, and,日期查询

db.getCollection('cabinTermRecord').find({ "$and" : [ { "$or" : [ { "personnelName" : { "$regex" : ".*能.*"}} , { "termMac" : { "$regex" : ".*能.*"}}]} , { "departureTime" : { "$gte" : 1634692496461 , "$lt" : 1634693096108}}]}).pretty()

 

java代码

//1.正则模糊查询   
Criteria criteria1 = new Criteria();
criteria1=criteria1.orOperator(Criteria.where("personnelName").regex(".*" + query.get("content") + ".*"),
                    Criteria.where("termMac").regex(".*" + query.get("content") + ".*"));
//Criteria.where("termMac").regex(".*bbc.*"));

//2.日期
Criteria criteria2= new Criteria();
criteria2= Criteria.where("departureTime").
is(BasicDBObjectBuilder.start("$gte", gteNum).
add("$lt", ltNum).get());

//3.and
Query dbQuery=new Query();
Criteria criteria = new Criteria();
criteria.andOperator(criteria1,criteria2);
dbQuery.addCriteria(criteria);

//4.or
Criteria criteria1 = new Criteria();
criteria1=criteria1.orOperator(Criteria.where("personnelName").regex(".*" + query.get("content") + ".*"),
                    Criteria.where("termMac").regex(".*" + query.get("content") + ".*"));
//criteria1.orOperator(criteria1,criteria2);

 

posted @ 2021-10-20 11:42  回溯者  阅读(1245)  评论(0编辑  收藏  举报