栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Spring Data中执行Mongo聚合查询?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Spring Data中执行Mongo聚合查询?

您可以实现AggregationOperation
并编写自定义聚合操作查询,然后用于

MongoTemplate
执行您在mongo shell中执行的任何mongo shell查询,如下所示:

自定义汇总操作

import org.springframework.data.mongodb.core.aggregation.AggregationOperation;import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;public class CustomAggregationOperation implements AggregationOperation {  private String jsonOperation;  public CustomAggregationOperation(String jsonOperation) {    this.jsonOperation = jsonOperation;  }  @Override  public org.bson.document todocument(AggregationOperationContext aggregationOperationContext) {    return aggregationOperationContext.getMappedObject(org.bson.document.parse(jsonOperation));  }}

任何Mongo Shell聚合查询执行程序

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.aggregation.Aggregation;import org.springframework.data.mongodb.core.aggregation.AggregationOperation;import org.springframework.data.mongodb.core.aggregation.AggregationResults;import org.springframework.data.mongodb.core.aggregation.TypedAggregation;import org.springframework.stereotype.Service;import sample.data.mongo.models.Course;@Servicepublic class LookupAggregation {  @Autowired  MongoTemplate mongoTemplate;  public void LookupAggregationExample() {    AggregationOperation unwind = Aggregation.unwind("studentIds");    String query1 = "{$lookup: {from: 'student', let: { stuId: { $toObjectId: '$studentIds' } },"        + "pipeline: [{$match: {$expr: { $eq: [ '$_id', '$$stuId' ] },},}, "        + "{$project: {isSendTemplate: 1,openId: 1,stu_name: '$name',stu_id: '$_id',},},], "        + "as: 'student',}, }";    TypedAggregation<Course> aggregation = Aggregation.newAggregation(        Course.class,        unwind,        new CustomAggregationOperation(query1)    );    AggregationResults<Course> results =        mongoTemplate.aggregate(aggregation, Course.class);    System.out.println(results.getMappedResults());  }}

有关更多详细信息,请查看Github存储库类:
CustomAggregationOperationLookupAggregation

其他 也使用 MongoTemplate的方法

#1。 为您的Model Post的 自定义代码定义一个接口:

interface CustomPostRepository {     List<Post> yourCustomMethod();}

#2。 添加此类的实现并遵循命名约定以确保我们可以找到该类。

class CustomPostRepositoryImpl implements CustomPostRepository {    @Autowired    private MongoOperations mongoOperations;    public List<Post> yourCustomMethod() {      // custom match queries here      MatchOperation match = null;      // Group by , Lookup others stuff goes here      // For details: https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/aggregation/Aggregation.html      Aggregation aggregate = Aggregation.newAggregation(match);      AggregationResults<Post> orderAggregate = mongoOperations.aggregate(aggregate,Post.class, Post.class);      return orderAggregate.getMappedResults();    }}

#3。 现在,让您的基本存储库界面扩展自定义界面,基础结构将自动使用您的自定义实现:

interface PostRepository extends CrudRepository<Post, Long>, CustomPostRepository {}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419320.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号