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