• mongodb操作二


    {
        "_id" : ObjectId("5d4d74e1685764420c4f9337"),
        "createTime" : ISODate("2019-08-09T13:25:33.851Z"),
        "modifiedTime" : ISODate("2019-08-09T13:25:33.851Z"),
        "isDeleted" : 0,
        "dateYmd" : "2019-08-09",
        "memberNumber" : "10025",
        "username" : "小杨",
        "attendenceTime" : ISODate("2019-08-09T13:25:33.851Z"),
        "orgEntity" : {
            "schoolId" : "f11c8ea12f457dbc19c768a8bb6357f8",
            "schoolName" : "体验学院",
            "depName" : "计算机学院",
            "depId" : "5ca65e204a75670545bfe67418c1fd00",
            "majorId" : "ca46da02e825dcf2ceb48a67e3a888db",
            "classId" : "87108cd7b71775fdf45a220eb734b6be",
            "majorField" : ".net开发",
            "majorName" : "计算机应用技术",
            "className" : "java一班"
            "studentId" : "029f7fd62186bfd1e9b2047f76095361",
            "userName" : "小杨",
            "snowFlakeId" : NumberLong(120986237900296192),
            "studentNumber" : "10025",
            "grade" : "2018"
        },
        "teacherId" : "1fbb4bd971550b540e2fb58b3f1efe52"
    }

    mongodb 表 mogu_attendance 的集合如上:

    按照  orgEntity 中的字段比如  schoolId 查询:

    mongodb查询如下:

    db.getCollection('mogu_attendance').find( { "orgEntity.schoolId" : "f11c8ea12f457dbc19c768a8bb6357f8",
    "orgEntity.classId" : { "$in" : ["5a05540e9fd1ce8604015f3fa5067b63",
    "61a582659385c9b91033bad877609758", "70ef8baad5d97ae7b77c6df3038b0d84",
    "87108cd7b71775fdf45a220eb734b6be"] },
    "$and" : [{ "attendenceTime" : { "$gte" : ISODate("2018-09-12T06:11:47.000Z") } },
    { "attendenceTime" : { "$lte" : ISODate("2029-09-22T06:11:47.000Z") } }] })

    在java实体中,需要把 需要查询的字段如 schoolId 放到实体中:

    @Data
    public class MoguAttendenceEntity extends BaseEntity implements Serializable {
        private static final long serialVersionUID = 1L;
    
        /**
         * 唯一主键
         */
    
        private String attendanceId;
        
        /**
         * 学校ID
         */
        private String schoolId;
    
        
        /**
         * 是否补卡
         */
        private Integer isReplace;
    
        private List<MongoGxyPracticeLogDto> logDtoList;
        /**
         * 入参
         */
        private Date startTime;
        private Date endTime;
        private String comment;
        private List<String> attendenceIds;
        private Integer applyState;
        private String teaId;
        private Date dateTime;
    
        private String depId;
        private String majorId;
        private String classId;
        private String userId;
        private Long snowFlakeId;
        /**
         * 筛选天数条件(1 连续3天 2七天 3一个月)
         */
        private Integer dayType;
        /**
         * 表名
         */
        private  String attendencdTabName;
        private String stuTabName;
        private String roleKey;
        private List<String> depIds;
        private List<String> classIds;
    
        /**
         * 考勤时间
         */
        private Date attendenceTime;
    
        private List<MoguAttendenceEntity> attendenceDtos;
    
        private String backup;
    
        private OrgEntity orgEntity;
    
        private String studentId;
    
        private String teacherId;
    
        private String grade;

      private List<String> classIds;
    }

    代码如下:

    moguAttendenceEntity.setClassIds(getClassIds(request));
    public List<MongoMoguAttendenceDto> listAttendance(MoguAttendenceEntity attendenceEntity){
    Sort sort = new Sort(Sort.Direction.ASC,"attendenceTime");
    Query query = new Query();
    Criteria criteria = new Criteria();
    if (attendenceEntity.getCurrPage() != 0 && attendenceEntity.getPageSize() != 0) {
    query.skip((attendenceEntity.getCurrPage()-1)*attendenceEntity.getPageSize())
    .limit(attendenceEntity.getPageSize());
    }
    if(StrUtil.isNotEmpty(attendenceEntity.getDepId())){
    criteria.and("orgEntity.depId").is(attendenceEntity.getDepId());
    }
    if(StrUtil.isNotEmpty(attendenceEntity.getGrade())){
    criteria.and("orgEntity.grade").is(attendenceEntity.getGrade());
    }
    if(StrUtil.isNotEmpty(attendenceEntity.getUsername())){
    criteria.and("orgEntity.userName").is(attendenceEntity.getUsername());
    }
    if(StrUtil.isNotEmpty(attendenceEntity.getStudentNumber())){
    criteria.and("orgEntity.studentNumber").is(attendenceEntity.getStudentNumber());
    }
    if(StrUtil.isNotEmpty(attendenceEntity.getSchoolId())){
    criteria.and("orgEntity.schoolId").is(attendenceEntity.getSchoolId());
    }

    if(StrUtil.isNotEmpty(attendenceEntity.getStuId())){
    criteria.and("memberId").is(attendenceEntity.getStuId());
    }

    if(attendenceEntity.getDepIds()!=null && attendenceEntity.getDepIds().size()>0 ){
    criteria.and("orgEntity.depId").in(attendenceEntity.getDepIds());
    }

    if(attendenceEntity.getClassIds()!=null && attendenceEntity.getClassIds().size()>0 ){
    criteria.and("orgEntity.classId").in(attendenceEntity.getClassIds());
    }

    if(attendenceEntity.getTeacherId()!=null ){
    criteria.and("teacherId").is(attendenceEntity.getTeacherId());
    }

    if(!StringUtils.isEmpty(attendenceEntity.getStartTime()) && !StringUtils.isEmpty(attendenceEntity.getEndTime())){
    criteria.andOperator(
    Criteria.where("attendenceTime").gte(attendenceEntity.getStartTime()),
    Criteria.where("attendenceTime").lte(attendenceEntity.getEndTime())
    );
    }

    query.addCriteria(criteria);
    query.with(sort);
    List<MongoMoguAttendenceDto> data = mongoTemplate.find(query,MongoMoguAttendenceDto.class);

    return data;
    }



    聚合查询:

    db.getCollection("mogu_attendance").aggregate(
        [
            { 
                "$match" : {
                    "attendanceType" : { $exists : false }, 
                    "state" : "NORMAL",
            "$and" : [{ "attendenceTime" : { "$gte" : ISODate("2019-09-15T06:11:47.000Z") } },
                     { "attendenceTime" : { "$lte" : ISODate("2019-09-16T06:11:47.000Z") } }]
                }
            }, 
            { 
                "$group" : {
                    "_id" : {
                        "memberId" : "$memberId",
                        "planId" : "$planId",
              "attendenceTime" : "$attendenceTime",
                         "attendanceType" : "$attendanceType", 
                        "state" : "$state"
                    }, 
                    "COUNT_PID" : {
                        "$sum" : 1.0
                    }
                }
            }
        ]
    )

    mongoDB批量插入:

    public int batchsave(List<NoticeSentRecordDto> objeceList){
    template.insert(objeceList,NoticeSentRecordDto.class);
    return 1;
    }


    二 mongodb 模糊查询:按照 orgEntity 下面的 userName 模糊查询, orgEntity.schoolId 精确查询:

    db.getCollection('mogu_attendance').find({ "orgEntity.userName" : { "$regex" : "梦", "$options" : "" },
    "orgEntity.schoolId" : "f11c8ea12f457dbc19c768a8bb6357f8" })

    Sort sort = new Sort(Sort.Direction.DESC,"attendenceTime");
    Query query = new Query();
    Criteria criteria = new Criteria();
    if (attendenceEntity.getCurrPage() != 0 && attendenceEntity.getPageSize() != 0) {
    query.skip((attendenceEntity.getCurrPage()-1)*attendenceEntity.getPageSize())
    .limit(attendenceEntity.getPageSize());
    }
    if(StrUtil.isNotEmpty(attendenceEntity.getUsername())){
    criteria.and("orgEntity.userName").regex(attendenceEntity.getUsername());
    }
    if(StrUtil.isNotEmpty(attendenceEntity.getSchoolId())){
    criteria.and("orgEntity.schoolId").is(attendenceEntity.getSchoolId());
    }
    query.addCriteria(criteria);
    query.with(sort);
    List<MongoMoguAttendenceDto> data = mongoTemplate.find(query,MongoMoguAttendenceDto.class);

    mongodb分页 or 查询:

    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.getStudentName()!=null){
                cri.orOperator(Criteria.where("studentName").regex(gxyWarnEntity.getStudentName()),
                        Criteria.where("teacherName").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);
        }

     删除集合:

    template.dropCollection(View.class);
  • 相关阅读:
    洛谷 P1525 关押罪犯(并查集|二分图判定&二分答案)
    洛谷 P1948 [USACO08JAN]Telephone Lines S(贪心+最短路)
    洛谷 P1315 观光公交(贪心+模拟)
    洛谷 P3258 [JLOI2014]松鼠的新家(树上差分)
    【NOIP2001】统计单词个数
    【洛谷习题】皇后游戏
    【洛谷习题】木棍加工
    【SDOI2008】仪仗队
    【洛谷习题】末日的传说
    【洛谷习题】又是毕业季I
  • 原文地址:https://www.cnblogs.com/z360519549/p/11520007.html
Copyright © 2020-2023  润新知