当前位置:   article > 正文

mongoDB 关联查询_lookupoperation.localfield.foreignfield

lookupoperation.localfield.foreignfield

mongoDB 关联查询

 

 

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

相关类与接口

 

lookUpOperation

  1. public class LookupOperation implements FieldsExposingAggregationOperation, InheritsFieldsAggregationOperation {
  2. private final Field from;
  3. private final Field localField;
  4. private final Field foreignField;
  5. private final ExposedField as;
  6. **********
  7. 构造方法
  8. public LookupOperation(Field from, Field localField, Field foreignField, Field as) {
  9. **********
  10. 常用方法
  11. public static LookupOperation.FromBuilder newLookup() {
  12. return new LookupOperation.LookupOperationBuilder();
  13. }
  14. **********
  15. 内部类
  16. public static final class LookupOperationBuilder implements LookupOperation.FromBuilder, LookupOperation.LocalFieldBuilder, LookupOperation.ForeignFieldBuilder, LookupOperation.AsBuilder {
  17. @Nullable
  18. private Field from;
  19. @Nullable
  20. private Field localField;
  21. @Nullable
  22. private Field foreignField;
  23. @Nullable
  24. private ExposedField as;
  25. public LookupOperationBuilder() {
  26. }
  27. public static LookupOperation.FromBuilder newBuilder() {
  28. return new LookupOperation.LookupOperationBuilder();
  29. }
  30. public LookupOperation.LocalFieldBuilder from(String name) {
  31. Assert.hasText(name, "'From' must not be null or empty!");
  32. this.from = Fields.field(name);
  33. return this;
  34. }
  35. public LookupOperation as(String name) {
  36. Assert.hasText(name, "'As' must not be null or empty!");
  37. this.as = new ExposedField(Fields.field(name), true);
  38. return new LookupOperation(this.from, this.localField, this.foreignField, this.as);
  39. }
  40. public LookupOperation.AsBuilder foreignField(String name) {
  41. Assert.hasText(name, "'ForeignField' must not be null or empty!");
  42. this.foreignField = Fields.field(name);
  43. return this;
  44. }
  45. public LookupOperation.ForeignFieldBuilder localField(String name) {
  46. Assert.hasText(name, "'LocalField' must not be null or empty!");
  47. this.localField = Fields.field(name);
  48. return this;
  49. }
  50. }
  51. **********
  52. 需要关联的集合
  53. public interface FromBuilder {
  54. LookupOperation.LocalFieldBuilder from(String var1);
  55. }
  56. **********
  57. 本地集合的关联键
  58. public interface LocalFieldBuilder {
  59. LookupOperation.ForeignFieldBuilder localField(String var1);
  60. }
  61. **********
  62. 需要关联集合对应的键
  63. public interface ForeignFieldBuilder {
  64. LookupOperation.AsBuilder foreignField(String var1);
  65. }
  66. **********
  67. 给关联集合的查询结果取别名
  68. public interface AsBuilder {
  69. LookupOperation as(String var1);
  70. }
  71. }

 

 

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

示例

 

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

controller 层

 

HelloController

  1. @RestController
  2. public class HelloController {
  3. @Autowired
  4. private MongoTemplate mongoTemplate;
  5. @RequestMapping("/save")
  6. public String save(){
  7. for(int i=0;i<100;i++){
  8. Student student=new Student();
  9. student.setId(i);
  10. student.setName("瓜田李下"+i);
  11. student.setAge(i%10+15);
  12. student.setSchoolId(i%3);
  13. mongoTemplate.save(student);
  14. }
  15. for (int i=0;i<3;i++){
  16. School school=new School();
  17. school.setId(i);
  18. school.setName("海贼王"+i);
  19. mongoTemplate.save(school);
  20. }
  21. return "success";
  22. }
  23. @RequestMapping("/get")
  24. public List<Map> get(){ //查询结果以Map形式返回
  25. LookupOperation lookupOperation=LookupOperation.newLookup()
  26. .from("school")
  27. .localField("schoolId")
  28. .foreignField("_id")
  29. .as("school");
  30. Aggregation aggregation=Aggregation.newAggregation(lookupOperation);
  31. AggregationResults<Map> results=mongoTemplate.aggregate(aggregation,"student",Map.class);
  32. return results.getMappedResults();
  33. }
  34. @RequestMapping("/get2")
  35. public List<StudentMap> get2(){ //自定义类接收查询结果
  36. LookupOperation lookupOperation=LookupOperation.newLookup()
  37. .from("school")
  38. .localField("schoolId")
  39. .foreignField("_id")
  40. .as("school");
  41. Aggregation aggregation=Aggregation.newAggregation(lookupOperation);
  42. AggregationResults results=mongoTemplate.aggregate(aggregation,"student",StudentMap.class);
  43. return results.getMappedResults();
  44. }
  45. @RequestMapping("/get3")
  46. public List<Map> get3(){ //查询结果以Map形式返回,分页排序
  47. LookupOperation lookupOperation=LookupOperation.newLookup()
  48. .from("school")
  49. .localField("schoolId")
  50. .foreignField("_id")
  51. .as("school");
  52. Aggregation aggregation=Aggregation.newAggregation(lookupOperation,Aggregation.skip(10L),Aggregation.limit(10L),Aggregation.sort(Sort.by("age").descending()));
  53. AggregationResults<Map> results=mongoTemplate.aggregate(aggregation,"student",Map.class);
  54. return results.getMappedResults();
  55. }
  56. @RequestMapping("/get4")
  57. public List<StudentMap> get4(){ //自定义类接收查询结果,分页排序
  58. LookupOperation lookupOperation=LookupOperation.newLookup()
  59. .from("school")
  60. .localField("schoolId")
  61. .foreignField("_id")
  62. .as("school");
  63. Aggregation aggregation=Aggregation.newAggregation(lookupOperation,Aggregation.skip(5L),Aggregation.limit(5L),Aggregation.sort(Sort.by("school").ascending()));
  64. AggregationResults results=mongoTemplate.aggregate(aggregation,"student",StudentMap.class);
  65. return results.getMappedResults();
  66. }
  67. }

 

 

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

使用测试

 

/get

                            

 

/get2

                            

 

/get3

                            

 

/get4

                              

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/56043
推荐阅读
相关标签
  

闽ICP备14008679号