ReplaceRoot 管道内的 MergeObjects Spring MongoDB
MergeObjects inside ReplaceRoot pipeline Spring MongoDB
我是 spring 的新人,我遇到了这种情况..
这是我的 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 对聚合的查询?
所以...我用下面的代码解决了这个问题!
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();
我是 spring 的新人,我遇到了这种情况.. 这是我的 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 对聚合的查询?
所以...我用下面的代码解决了这个问题!
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();