【问题标题】:Can someone help me with this有人可以帮我弄这个吗
【发布时间】:2022-01-17 10:42:50
【问题描述】:

我实际上需要在 mongoDb 中使用聚合。我已经使用以下查询加入两个集合,然后通过一些过滤器提取答案。查询在 mongodb 终端中运行良好。

但我需要在 Spring Boot 中使用 Aggreagte 函数在 Spring Boot 中使用此查询。我查看了以下文档,这非常有用。但是由于查询很大。这就是为什么我无法以正确的方式编写它。

https://www.baeldung.com/spring-data-mongodb-projections-aggregations

db.coll1.aggregate([
  {
    "$unionWith": {"coll": "coll2"}
  },
  {
    "$group": {
      "_id": {
        "Id": "Id",
        "Name": {
          "$arrayElemAt": [
            {
              $split: [
                "FullName",
                "T",
              ]
            },
            1
          ]
        },
        "StreetName": {
          "$arrayElemAt": [
            {
              $split: [
                "Address",
                "T",
                
              ]
            },
            1
          ]
        }
        
      },
      "count": {
        "$sum": 1
      }
    }
  },
  {
    "$match": {
      "$expr": {
        "$gt": [
          "$count",
          1
        ]
      }
    }
  }])

你能帮我使用aggreagte函数并转换这个查询,以便我可以在springboot中为你做这个。

【问题讨论】:

标签: mongodb spring-boot aggregation-framework spring-data-mongodb aggregation


【解决方案1】:

看起来像尝试进行计数(*),按 trunc(gapStartTimeStamp)、trunc(gapEndTimeStamp) 分组,其中 count > 1

如果是这样: 您的查询略有不同,使用 spring-data-mongodb 3.1.8 测试



UnionWithOperation unionWith = UnionWithOperation.unionWith("coll2");
        
ProjectionOperation convertStartDateOp = Aggregation.project("stationId", "gapStartTimeStamp", "gapEndTimeStamp")
            .and(StringOperators.Substr.valueOf("gapStartTimeStamp").substring(0, 10))
                .as("gapStartDate")
                .and(StringOperators.Substr.valueOf("gapEndTimeStamp").substring(0, 10))
                .as("gapEndDate");
                
GroupOperation countOp = Aggregation.group("stationId", "gapStartDate", "gapEndDate").count().as("count");
        MatchOperation matchOp = Aggregation.match(Criteria.where("count").gt(1));

AggregationResults<Document> aggregate = mongoOps.aggregate(
        Aggregation.newAggregation(
            unionWith,convertStartDateOp,countOp, matchOp
        ),
        YourColl1.class, Document.class);

List<Document> mappedResults = aggregate.getMappedResults();

生成这个查询:

db.coll1.aggregate(
[
 {"$unionWith": {"coll": "coll2"}}, 
  {"$project": 
    {
      "stationId": 1, 
      "gapStartTimeStamp": 1, 
      "gapEndTimeStamp": 1, 
      "gapStartDate": {"$substr": ["$gapStartTimeStamp", 0, 10]}, 
      "gapEndDate": {"$substr": ["$gapEndTimeStamp", 0, 10]}
    }
  }, 
  {"$group": {
    "_id": {
      "stationId": "$stationId", 
      "gapStartDate": "$gapStartDate", 
      "gapEndDate": "$gapEndDate"
    }, 
    "count": {"$sum": 1}
   }
  }, 
  {"$match": {"count": {"$gt": 1}}}
  
  ]
  );

【讨论】:

  • 聚合部分中的“mongoOps”代表什么?
  • mongoOps 来自org.springframework.data.mongodb.core.MongoOperations
  • 是的 YourColl1.class 应该代表 coll1 集合的 mongodb 数据模型类
  • 来自org.bson.Document
  • 不允许对该评论进行编辑 - 整个评论已被删除
猜你喜欢
  • 2021-02-12
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
  • 2015-05-31
  • 2020-03-25
相关资源
最近更新 更多