首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于springdata mongodb聚合

关于springdata mongodb聚合
EN

Stack Overflow用户
提问于 2019-12-27 02:59:48
回答 1查看 123关注 0票数 0

目前,我有一个问题。我可以使用mongodb中的聚合函数来查询相应的数据,但是在使用springdatamongodb之后,我发现lookup不能使用变量将字符串转换为objectid,所以我应该如何编写这个聚合函数?

如何将它写成mongodb表达式,在spring数据莫加德中使用

代码语言:javascript
复制
  db.getCollection('course').aggregate([{
            $unwind: '$studentIds',
          },
          {
            $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',
            },
          }])
EN

回答 1

Stack Overflow用户

发布于 2019-12-28 04:10:46

对于复杂的自定义聚合查询,您可以创建一个自定义AggregationOperation,然后从Mongo传递聚合json,或者您拥有的内容。例如:

代码语言:javascript
复制
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));
  }
}

现在从聚合查询类中传递上面类中的$lookup查询,如下所示:

代码语言:javascript
复制
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;

@Service
public 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());
  }
}

要获得更多详细信息,您可以在:https://github.com/krishnaiitd/learningJava/blob/master/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/services/LookupAggregation.java上查看我的Github

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59495531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档