mongoTemplate關聯查詢



//主表c_stu_homework關聯兩張表c_course和c_category,localField()從表關聯條件,foreignField()主表的關聯條件,as()別名 LookupOperation courseLookupOperation = LookupOperation.newLookup(). from("c_course"). localField("courseId"). foreignField("id"). as("course"); LookupOperation categoryLookupOperation = LookupOperation.newLookup(). from("c_category"). localField("homewkId"). foreignField("homewkId"). as("category"); String startDate = DateUtils.dateToString1(new Date()) + " 00:00:00"; String endDate = DateUtils.dateToString1(new Date()) + " 23:59:59"; //條件和查詢的字段 Criteria cri = Criteria.where("createTime").gte(startDate).lte(endDate); AggregationOperation match = Aggregation.match(cri); ProjectionOperation project = Aggregation.project("id", "stuId", "homewkId", "homewkName", "category.categoryName", "completeStatus", "score", "createTime", "endTime", "courseId", "course.courseName", "orgId", "tchId", "tchName", "courseUnitId", "course.textbookId", "course.textbookVersionId", "course.textLevelId"); //將以上的條件封裝 List<AggregationOperation> operations = new ArrayList<>(); operations.add(courseLookupOperation); operations.add(categoryLookupOperation); operations.add(match); operations.add(project); Aggregation aggregation = Aggregation.newAggregation(operations); AggregationResults<StudentHomeEntity> results = mongoTemplate.aggregate(aggregation, "c_stu_homework", StudentHomeEntity.class); List<StudentHomeEntity> mappedResults = results.getMappedResults();
 
        
 
         
         
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM