创建聚合管道时,累加器操作器最大值出现问题

jum4pzuy  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(451)

我正在尝试将以下mongo shell聚合查询转换为spring数据mongodb聚合:

  1. db.getCollection('Orders').aggregate([
  2. { $group: {
  3. "_id": {"book":"$bookingId"},
  4. "docs": {$push: '$$ROOT'}
  5. }
  6. },
  7. {$project: {
  8. latestOrd: {
  9. $filter: {
  10. input: "$docs",
  11. as: "item",
  12. cond: { $eq: ["$$item.bookingVersion", { $max: "$docs.bookingVersion" }] }
  13. }
  14. }
  15. }
  16. },
  17. { $unwind: "$latestOrd" },
  18. { $replaceRoot:{newRoot:"$latestOrd"}}
  19. ])

查询获取具有最高预订版本的所有订单(例如,一个bookingid可能有许多具有版本3的文档)。
此查询在mongo shell中运行良好,但我对它的spring数据版本有异议:

  1. Aggregation.group("bookingId").push(Aggregation.ROOT).as("docs");
  2. Aggregation.project().and(filter("docs")
  3. .as("item")
  4. .by(valueOf("item.bookingVersion")
  5. .equalToValue(AccumulatorOperators.Max.maxOf("docs.bookingVersion"))))
  6. .as("latestOrd");
  7. Aggregation.unwind("latestOrd");
  8. Aggregation.replaceRoot("latestOrd");

spring生成的mongo查询与我上面提供的查询类似,除了$max accumulator:

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

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

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

我使用的是springbootstarter2.1.0.release和mongo服务器的4.2版本。我很感激你在这方面的帮助。
输入文件:

  1. [
  2. {
  3. "_id": "5f847811ebcd1a51a0196736",
  4. "status": "REJECTED",
  5. "bookingId": "1",
  6. "bookingVersion": 4,
  7. "operation": "CREATE"
  8. },
  9. {
  10. "_id": "5f847811ebcd1a51a0196735",
  11. "status": "CREATED",
  12. "bookingId": "1",
  13. "bookingVersion": 4,
  14. "docNumber": "7",
  15. "operation": "CREATE"
  16. },
  17. {
  18. "_id": "5f847811ebcd1a51a0196734",
  19. "status": "CREATED",
  20. "bookingId": "1",
  21. "bookingVersion": 3,
  22. "docNumber": "6",
  23. "operation": "CREATE"
  24. },
  25. {
  26. "_id": "5f847811ebcd1a51a0196738",
  27. "status": "CREATED",
  28. "bookingId": "2",
  29. "bookingVersion": 1,
  30. "docNumber": "8",
  31. "operation": "CREATE"
  32. },
  33. {
  34. "_id": "5f847811ebcd1a51a0196737",
  35. "status": "CREATED",
  36. "bookingId": "2",
  37. "bookingVersion": 2,
  38. "docNumber": "9",
  39. "operation": "CREATE"
  40. }
  41. ]

预期产量:

  1. [
  2. {
  3. "_id": "5f847811ebcd1a51a0196736",
  4. "status": "REJECTED",
  5. "bookingId": "1",
  6. "bookingVersion": 4,
  7. "operation": "CREATE"
  8. },
  9. {
  10. "_id": "5f847811ebcd1a51a0196735",
  11. "status": "CREATED",
  12. "bookingId": "1",
  13. "bookingVersion": 4,
  14. "docNumber": "7",
  15. "operation": "CREATE"
  16. },
  17. {
  18. "_id": "5f847811ebcd1a51a0196737",
  19. "status": "CREATED",
  20. "bookingId": "2",
  21. "bookingVersion": 2,
  22. "docNumber": "9",
  23. "operation": "CREATE"
  24. }
  25. ]
kuuvgm7e

kuuvgm7e1#

问题可能出在SpringBoot的2.1.0.release版本上,但升级它不是我的选择。为了实现这一点,我创建了aggregationexpression,而不是 AccumulatorOperators.Max.maxOf("docs.bookingVersion") 部分。
这里有一个方法:

  1. private AggregationExpression buildMaxExpression() {
  2. return new AggregationExpression() {
  3. @Override
  4. public Document toDocument(final AggregationOperationContext context) {
  5. return new Document("$max", "$docs.bookingVersion");
  6. }
  7. };
  8. }

相关问题