mongoDB 关联查询


mongoDB 关联查询

 

 

*************************

相关类与接口

 

lookUpOperation

public class LookupOperation implements FieldsExposingAggregationOperation, InheritsFieldsAggregationOperation {
    private final Field from;
    private final Field localField;
    private final Field foreignField;
    private final ExposedField as;

**********
构造方法

    public LookupOperation(Field from, Field localField, Field foreignField, Field as) {


**********
常用方法

    public static LookupOperation.FromBuilder newLookup() {
        return new LookupOperation.LookupOperationBuilder();
    }



**********
内部类

    public static final class LookupOperationBuilder implements LookupOperation.FromBuilder, LookupOperation.LocalFieldBuilder, LookupOperation.ForeignFieldBuilder, LookupOperation.AsBuilder {
        @Nullable
        private Field from;
        @Nullable
        private Field localField;
        @Nullable
        private Field foreignField;
        @Nullable
        private ExposedField as;

        public LookupOperationBuilder() {
        }

        public static LookupOperation.FromBuilder newBuilder() {
            return new LookupOperation.LookupOperationBuilder();
        }

        public LookupOperation.LocalFieldBuilder from(String name) {
            Assert.hasText(name, "'From' must not be null or empty!");
            this.from = Fields.field(name);
            return this;
        }

        public LookupOperation as(String name) {
            Assert.hasText(name, "'As' must not be null or empty!");
            this.as = new ExposedField(Fields.field(name), true);
            return new LookupOperation(this.from, this.localField, this.foreignField, this.as);
        }

        public LookupOperation.AsBuilder foreignField(String name) {
            Assert.hasText(name, "'ForeignField' must not be null or empty!");
            this.foreignField = Fields.field(name);
            return this;
        }

        public LookupOperation.ForeignFieldBuilder localField(String name) {
            Assert.hasText(name, "'LocalField' must not be null or empty!");
            this.localField = Fields.field(name);
            return this;
        }
    }

**********
需要关联的集合

    public interface FromBuilder {
        LookupOperation.LocalFieldBuilder from(String var1);
    }


**********
本地集合的关联键

    public interface LocalFieldBuilder {
        LookupOperation.ForeignFieldBuilder localField(String var1);
    }


**********
需要关联集合对应的键

    public interface ForeignFieldBuilder {
        LookupOperation.AsBuilder foreignField(String var1);
    }


**********
给关联集合的查询结果取别名

    public interface AsBuilder {
        LookupOperation as(String var1);
    }

}

 

 

*************************

示例

 

******************

controller 层

 

HelloController

@RestController
public class HelloController {

    @Autowired
    private MongoTemplate mongoTemplate;

    @RequestMapping("/save")
    public String save(){
        for(int i=0;i<100;i++){
            Student student=new Student();
            student.setId(i);
            student.setName("瓜田李下"+i);
            student.setAge(i%10+15);
            student.setSchoolId(i%3);

            mongoTemplate.save(student);
        }

        for (int i=0;i<3;i++){
            School school=new School();
            school.setId(i);
            school.setName("海贼王"+i);

            mongoTemplate.save(school);
        }

        return "success";
    }

    @RequestMapping("/get")
    public List<Map> get(){            //查询结果以Map形式返回
        LookupOperation lookupOperation=LookupOperation.newLookup()
                .from("school")
                .localField("schoolId")
                .foreignField("_id")
                .as("school");

        Aggregation aggregation=Aggregation.newAggregation(lookupOperation);
        AggregationResults<Map> results=mongoTemplate.aggregate(aggregation,"student",Map.class);

        return results.getMappedResults();
    }

    @RequestMapping("/get2")
    public List<StudentMap> get2(){    //自定义类接收查询结果
        LookupOperation lookupOperation=LookupOperation.newLookup()
                .from("school")
                .localField("schoolId")
                .foreignField("_id")
                .as("school");

        Aggregation aggregation=Aggregation.newAggregation(lookupOperation);
        AggregationResults results=mongoTemplate.aggregate(aggregation,"student",StudentMap.class);

        return results.getMappedResults();
    }

    @RequestMapping("/get3")
    public List<Map> get3(){        //查询结果以Map形式返回,分页排序
        LookupOperation lookupOperation=LookupOperation.newLookup()
                .from("school")
                .localField("schoolId")
                .foreignField("_id")
                .as("school");

        Aggregation aggregation=Aggregation.newAggregation(lookupOperation,Aggregation.skip(10L),Aggregation.limit(10L),Aggregation.sort(Sort.by("age").descending()));
        AggregationResults<Map> results=mongoTemplate.aggregate(aggregation,"student",Map.class);

        return results.getMappedResults();
    }

    @RequestMapping("/get4")
    public List<StudentMap> get4(){       //自定义类接收查询结果,分页排序
        LookupOperation lookupOperation=LookupOperation.newLookup()
                .from("school")
                .localField("schoolId")
                .foreignField("_id")
                .as("school");

        Aggregation aggregation=Aggregation.newAggregation(lookupOperation,Aggregation.skip(5L),Aggregation.limit(5L),Aggregation.sort(Sort.by("school").ascending()));
        AggregationResults results=mongoTemplate.aggregate(aggregation,"student",StudentMap.class);

        return results.getMappedResults();
    }
}

 

 

*************************

使用测试

 

/get

                            

 

/get2

                            

 

/get3

                            

 

/get4

                              

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值