mongodb and 和 or 查询
db.getCollection('gxyWarnEntity').find({ "schoolId" : "f11c8ea12f457dbc19c768a8bb6357f8", "batchId" : "594f724d6974c107bd702cc6ef39183a", "$or" : [{ "studentName" : { "$regex" : "小", "$options" : "" } }, { "teacherName" : { "$regex" : "小", "$options" : "" } }] })
java:
public List<GxyWarnEntity> selectWarnPage(GxyWarnEntity gxyWarnEntity){ Criteria cri =Criteria.where("schoolId").is(gxyWarnEntity.getSchoolId()); if(gxyWarnEntity.getBatchId()!=null) { cri.and("batchId").is(gxyWarnEntity.getBatchId()); } if(gxyWarnEntity.getDepId()!=null){ cri.and("depId").is(gxyWarnEntity.getDepId()); } if(gxyWarnEntity.getWarningType()!=null){ cri.and("warningType").is(gxyWarnEntity.getWarningType()); } if(gxyWarnEntity.getStudentName()!=null){ cri.orOperator(Criteria.where("studentName").regex(gxyWarnEntity.getStudentName()), Criteria.where("teacherName").regex(gxyWarnEntity.getStudentName()) //,Criteria.where("classTeacherName").regex(gxyWarnEntity.getStudentName()) ); } Query query = new Query(cri); if (gxyWarnEntity.getCurrPage() != null && gxyWarnEntity.getPageSize() != null) { query.skip((gxyWarnEntity.getCurrPage()-1)*gxyWarnEntity.getPageSize()) .limit(gxyWarnEntity.getPageSize()); } return template.find(query, GxyWarnEntity.class); }
and:
db.getCollection('gxyWarnEntity').find({ "schoolId" : "f11c8ea12f457dbc19c768a8bb6357f8", "batchId" : "", "$and" : [{ "warningType" : { "$gte" : 1 } }, { "warningType" : { "$ne" : 4 } }], "$or" : [{ "studentName" : { "$regex" : "小", "$options" : "" } }, { "teacherName" : { "$regex" : "小", "$options" : "" } }] })
{ "schoolId" : "f11c8ea12f457dbc19c768a8bb6357f8", "batchId" : "", "$and" : [{ "warningType" : { "$gte" : 1 } }, { "warningType" : { "$ne" : 4 } }], "$or" : [{ "studentName" : { "$regex" : "小", "$options" : "" } }, { "teacherName" : { "$regex" : "小", "$options" : "" } }] } fields: Document{{}} for class: class com.zhangtao.moguding.practiceservice.entity.GxyWarnEntity in collection: gxyWarnEntity
2019-10-15 09:52:17.819 DEBUG 13028 --- [io-9020-exec-15] o.s.data.mongodb.core.MongoTemplate : Executing count: { "schoolId" : "f11c8ea12f457dbc19c768a8bb6357f8", "batchId" : "", "$and" : [{ "warningType" : { "$gte" : 1 } }, { "warningType" : { "$ne" : 4 } }], "$or" : [{ "studentName" : { "$regex" : "小", "$options" : "" } }, { "teacherName" : { "$regex" : "小", "$options" : "" } }] } in collection: gxyWarnEntity
public List<GxyWarnEntity> selectWarnPage(GxyWarnEntity gxyWarnEntity){ Criteria cri =Criteria.where("schoolId").is(gxyWarnEntity.getSchoolId()); if(gxyWarnEntity.getBatchId()!=null) { cri.and("batchId").is(gxyWarnEntity.getBatchId()); } if(gxyWarnEntity.getDepId()!=null){ cri.and("depId").is(gxyWarnEntity.getDepId()); } if(gxyWarnEntity.getWarningType()!=null && gxyWarnEntity.getWarningType() != EnumUnsignType.UNSIGNONE.getType()){ cri.andOperator( Criteria.where("warningType").gte(gxyWarnEntity.getWarningType()), Criteria.where("warningType").ne(EnumUnsignType.UNSIGNONE.getType()) ); } if(gxyWarnEntity.getStudentName()!=null){ cri.orOperator(Criteria.where("studentName").regex(gxyWarnEntity.getStudentName()), Criteria.where("teacherName").regex(gxyWarnEntity.getStudentName()) //,Criteria.where("classTeacherName").regex(gxyWarnEntity.getStudentName()) ); } Query query = new Query(cri); if (gxyWarnEntity.getCurrPage() != null && gxyWarnEntity.getPageSize() != null) { query.skip((gxyWarnEntity.getCurrPage()-1)*gxyWarnEntity.getPageSize()) .limit(gxyWarnEntity.getPageSize()); } return template.find(query, GxyWarnEntity.class); }