I want to run multiple facets in one aggregation to save db round trips. Here is my spring data code :
final BalancesDTO total
= this.mongoTemplate.aggregate(
newAggregation(
/*
* Get all fund transactions for this user
*/
match(where("userId").is(userId)),
/*
* Summarize Confirmed Debits
*/
facet( match(where("entryType").is(EntryType.DEBIT)
.andOperator(where("currentStatus").is(TransactionStatus.CONFIRMED))),
unwind("history"),
match(where("history.status").is(TransactionStatus.CONFIRMED)),
project().andExpression("history.amount").as("historyAmount"),
group().sum("historyAmount").as("total"),
project("total")
).as("totalConfirmedDebits"),
/*
* Summarize Confirmed Credits
*/
facet( match(where("entryType").is(EntryType.CREDIT)
.andOperator(where("currentStatus").is(TransactionStatus.CONFIRMED))),
unwind("history"),
match(where("history.status").is(TransactionStatus.CONFIRMED)),
project().andExpression("history.amount").as("historyAmount"),
group().sum("historyAmount").as("total"),
project("total")
).as("totalConfirmedCredits")
),
FundTransactions.class,
BalancesDTO.class)
.getUniqueMappedResult();
LOGGER.debug("total : {}",total.getTotalConfirmedDebits().get(0).getTotal());
When I run the above code it gives me below exception:
java.lang.IllegalArgumentException: Invalid reference 'history'!
at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:100) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:72) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.mongodb.core.aggregation.UnwindOperation.toDocument(UnwindOperation.java:94) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.mongodb.core.aggregation.AggregationOperationRenderer.toDocument(AggregationOperationRenderer.java:55) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.mongodb.core.aggregation.FacetOperation$Facet.toDocuments(FacetOperation.java:224) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.mongodb.core.aggregation.FacetOperation$Facets.toDocument(FacetOperation.java:168) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.mongodb.core.aggregation.FacetOperation.toDocument(FacetOperation.java:87) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.mongodb.core.aggregation.AggregationOperationRenderer.toDocument(AggregationOperationRenderer.java:55) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.mongodb.core.aggregation.Aggregation.toDocument(Aggregation.java:585) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate$BatchAggregationLoader.prepareAggregationCommand(MongoTemplate.java:3124) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate$BatchAggregationLoader.aggregate(MongoTemplate.java:3107) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1937) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1832) ~[spring-data-mongodb-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at io.tradehack.shared.fundtransactions.FundTransactionsRepositoryImpl.getFundBalances(FundTransactionsRepositoryImpl.java:41) ~[classes/:na]
at io.tradehack.shared.fundtransactions.FundTransactionsRepositoryImpl$$FastClassBySpringCGLIB$$89f7e1a0.invoke(<generated>) ~[classes/:na]
But the funny thing is if I removed the second facet section it works. What could be the reason for this exception?
Below is the equivalent mongodb command I want to run:
db.runCommand(
{ "aggregate" : "FundTransactions",
"pipeline" : [{ "$match" : { "userId" : "6cd984b9-1c17-402b-be9c-70614e0b8b8e"} },
{ "$facet" : { "totalConfirmedDebits" : [{ "$match" : { "entryType" : "DEBIT",
"$and" : [{ "currentStatus" : "CONFIRMED" }] } },
{ "$unwind" : "$history" },
{ "$match" : { "history.status" : "CONFIRMED" } },
{ "$project" : { "historyAmount" : "$history.amount" } },
{ "$group" : { "_id" : null,
"total" : { "$sum" : "$historyAmount" } } },
{ "$project" : { "total" : 1 } }],
"totalConfirmedCredits" : [{ "$match" : { "entryType" : "CREDIT",
"$and" : [{ "currentStatus" : "CONFIRMED" }] } },
{ "$unwind" : "$history" },
{ "$match" : { "history.status" : "CONFIRMED" } },
{ "$project" : { "historyAmount" : "$history.amount" } },
{ "$group" : { "_id" : null,
"total" : { "$sum" : "$historyAmount" } } },
{ "$project" : { "total" : 1 } }]
}
}],
"cursor" : { "batchSize" : 2147483647 } }
)
And below is the sample data:
{
"_id" : "dfe9dd63-6689-4e9f-8494-24efa6191db1",
"userId" : "6cd984b9-1c17-402b-be9c-70614e0b8b8e",
"entryType" : "DEBIT",
"type" : "DEPOSIT",
"createdDateTime" : ISODate("2018-11-11T03:00:00.000+00:00"),
"currentVersion" : 2,
"currentStatus" : "CONFIRMED",
"history" : [
{
"referenceId" : null,
"currency" : "USD",
"amount" : 1000000,
"comments" : "Initial fundings",
"updatedDateTime" : ISODate("2018-11-11T03:00:00.000+00:00"),
"status" : "PENDING",
"version" : 1
},
{
"referenceId" : null,
"currency" : "USD",
"amount" : 1001000,
"comments" : "Initial fundings",
"updatedDateTime" : ISODate("2018-11-11T04:00:00.000+00:00"),
"status" : "CONFIRMED",
"version" : 2
}
]
}
Any help will be greatly appreciated.