【发布时间】: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