【问题标题】:Issue with AccumulatorOperators Max when creating aggregation pipeline创建聚合管道时,AccumulatorOperators Max 出现问题
【发布时间】:2021-04-02 02:54:31
【问题描述】:

我正在尝试将以下 Mongo shell 聚合查询转换为 Spring Data MongoDB 聚合:

db.getCollection('Orders').aggregate([
{ $group: {
    "_id": {"book":"$bookingId"},
    "docs": {$push: '$$ROOT'}
  }
},
{$project: {
   latestOrd: {
      $filter: {
         input: "$docs",
         as: "item",
         cond: { $eq: ["$$item.bookingVersion", { $max: "$docs.bookingVersion" }] }
      }
   }
 }
},
{ $unwind: "$latestOrd" },
{ $replaceRoot:{newRoot:"$latestOrd"}}
])

查询获取所有具有最高预订版本的订单(例如,一个 bookingId 可能有许多具有版本 3 的文档)。

此查询在 Mongo shell 中运行良好,但我对它的 Spring Data 版本有疑问:

Aggregation.group("bookingId").push(Aggregation.ROOT).as("docs");
Aggregation.project().and(filter("docs")
          .as("item")
          .by(valueOf("item.bookingVersion")
                .equalToValue(AccumulatorOperators.Max.maxOf("docs.bookingVersion"))))
          .as("latestOrd");
Aggregation.unwind("latestOrd");
Aggregation.replaceRoot("latestOrd");

Spring 生成的 Mongo 查询类似于我在上面提供的查询,除了 $max 累加器:

{ "$max" : "$$docs.bookingVersion" }

由于某种原因,它添加了双美元符号而不是单美元符号,因此出现以下错误:

'Use of undefined variable: docs' on server 127.0.0.1:27017

我正在使用 spring-boot-starter 2.1.0.RELEASE 和 4.2 版本的 Mongo 服务器。 感谢您对此提供的任何帮助。

输入文件:

[
  {
    "_id": "5f847811ebcd1a51a0196736",
    "status": "REJECTED",
    "bookingId": "1",
    "bookingVersion": 4,
    "operation": "CREATE"
  },
  {
    "_id": "5f847811ebcd1a51a0196735",
    "status": "CREATED",
    "bookingId": "1",
    "bookingVersion": 4,
    "docNumber": "7",
    "operation": "CREATE"
  },
  {
    "_id": "5f847811ebcd1a51a0196734",
    "status": "CREATED",
    "bookingId": "1",
    "bookingVersion": 3,
    "docNumber": "6",
    "operation": "CREATE"
  },
  {
    "_id": "5f847811ebcd1a51a0196738",
    "status": "CREATED",
    "bookingId": "2",
    "bookingVersion": 1,
    "docNumber": "8",
    "operation": "CREATE"
  },
  {
    "_id": "5f847811ebcd1a51a0196737",
    "status": "CREATED",
    "bookingId": "2",
    "bookingVersion": 2,
    "docNumber": "9",
    "operation": "CREATE"
  }
]

预期输出:

[
  {
    "_id": "5f847811ebcd1a51a0196736",
    "status": "REJECTED",
    "bookingId": "1",
    "bookingVersion": 4,
    "operation": "CREATE"
  },
  {
    "_id": "5f847811ebcd1a51a0196735",
    "status": "CREATED",
    "bookingId": "1",
    "bookingVersion": 4,
    "docNumber": "7",
    "operation": "CREATE"
  },
  {
    "_id": "5f847811ebcd1a51a0196737",
    "status": "CREATED",
    "bookingId": "2",
    "bookingVersion": 2,
    "docNumber": "9",
    "operation": "CREATE"
  }
]

【问题讨论】:

  • 我使用 Spring boot starter 2.3.2.RELEASE 和 mongo 版本 4.0.10 进行了检查。它产生正确的查询并给出预期的输出。升级 Spring Boot 版本可能会解决您的问题。
  • 谢谢@Harshit,但升级 Spring Boot 版本对我来说不是一个选项。我将尝试使用 AggregationExpression 而不是 AccumulatorOperators.Max

标签: java mongodb spring-data spring-data-mongodb spring-mongodb


【解决方案1】:

问题可能与 Spring Boot 的 2.1.0.RELEASE 版本有关,但升级它不是我的选择。 为了完成这项工作,我创建了 AggregationExpression 而不是 AccumulatorOperators.Max.maxOf("docs.bookingVersion") 部分。

这是一个方法:

  private AggregationExpression buildMaxExpression() {
    return new AggregationExpression() {
      @Override
      public Document toDocument(final AggregationOperationContext context) {
        return new Document("$max", "$docs.bookingVersion");
      }
    };
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 2020-04-28
    • 1970-01-01
    • 2011-02-16
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多