【问题标题】:MergeObjects inside ReplaceRoot pipeline Spring MongoDBReplaceRoot 管道 Spring MongoDB 中的 MergeObjects
【发布时间】:2021-11-03 20:32:57
【问题描述】:

我是春天的新手,我有这种情况.. 这是我的 Json。

  {
    "_id": {
      "$oid": "60ba776d3ef89419f8668333"
    },
    "reference": "20210906164455",
    "transactionReference": "999999999999",
    "status": "PARTIALLY",
    "currency": "BRL",
    "amount": {
      "$numberDecimal": "99.80"
    },
    "ucode": "XXXXXXXXXXXXXXXXXXX",
    "refunds": [
      {
        "_id": {
          "$oid": "60ba77f03ef89419f8668337"
        },
        "currency": "BRL",
        "amount": {
          "$numberDecimal": "1.10"
        },
        "status": "PARTIALLY",
        "createDate": {
          "$date": "2021-06-04T18:58:57.145Z"
        }
      },
      {
        "_id": {
          "$oid": "60ba7b6d3ef89419f8668339"
        },
        "currency": "BRL",
        "amount": {
          "$numberDecimal": "10.10"
        },
        "status": "PARTIALLY",
        "createDate": {
          "$date": "2021-06-04T19:13:49.229Z"
        }
      }
    ],
    "confirmed": true,
    "createDate": {
      "$date": "2021-09-01T00:56:45.235Z"
    },
    "lastModifiedDate": {
      "$date": "2021-09-04T19:15:57.787Z"
    },
    "amountRefunded": {
      "$numberDecimal": "21.30"
    }
  }

我做了这个查询

db.collection.aggregate([
  {
    "$addFields": {
      "refunds": {
        "$concatArrays": [
          "$refunds",
          [
            {
              "amount": "$amount",
              "createDate": "$createDate"
            }
          ]
        ]
      }
    }
  },
  {
    "$unwind": {
      "path": "$refunds"
    }
  },
  {
    "$replaceRoot": {
      "newRoot": {
        "$mergeObjects": [
          "$$ROOT",
          "$refunds"
        ]
      }
    }
  },
  {
    "$unset": [
      "refunds"
    ]
  },
  {
    "$sort": {
      "createDate": -1
    }
  },
  {
    "$limit": 10
  }
])

现在我有两个我想要的对象。 Pratical example

所以现在我需要使用聚合将此代码传输到 Java。

我做了这个实现,但问题是...我丢失了其他值,只出现数组退款中的值。

AggregationOperation match = Aggregation.match(criteria);
AggregationOperation unwind = Aggregation.unwind("refunds");
AggregationOperation sort = Aggregation.sort(Sort.Direction.DESC, "createDate");
AggregationOperation replaceRoot = Aggregation.replaceRoot("refunds");
AggregationOperation limit = Aggregation.limit(20);

Aggregation aggregation = Aggregation.newAggregation(match, unwind,  sort, replaceRoot, limit);
List<Payments> paymentRefunds = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(Payments.class), Payments.class).getMappedResults(); 

如何将 Mongo 查询回复到聚合?

【问题讨论】:

    标签: java spring-boot spring-data-jpa aggregation-framework aggregation


    【解决方案1】:

    所以...我用下面的代码解决了这个问题!

    AggregationOperation match = Aggregation.match(criteria);
    AggregationOperation unwind = new UnwindOperation(Fields.field(refunds),true);
    AggregationOperation sort = Aggregation.sort(Sort.Direction.DESC, createDate);
    AggregationOperation limit = Aggregation.limit(request.getRows());
    AggregationOperation replaceRoot = ReplaceRootOperation.builder().withDocument(new Document("$mergeObjects", Arrays.asList(Aggregation.ROOT, "$refunds")));
    AggregationOperation addFieldAndConcatArrays = aoc -> new Document("$addFields", new Document(refunds, ArrayOperators.ConcatArrays.arrayOf(List.of(new Document(amount, "$amount"))).concat("$refunds").toDocument(aoc)));
    AggregationOperation unset = aoc -> new Document("$unset", refunds);
    
    Aggregation aggregation = Aggregation.newAggregation(match, addFieldAndConcatArrays, unwind, replaceRoot, unset, sort, limit);
    
    return mongoTemplate.aggregate(aggregation, Payment.class, Payment.class).getMappedResults();
    

    【讨论】:

      猜你喜欢
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多