La idea es unir los cobros de pago y devolución , transformarlo en un nuevo cobro llamado transacción, el cual tendrá el resultado de esta unión.
mi guion
db.payment.aggregate([ { $replaceRoot: { newRoot: { "_id": "$_id", "type": "payment", "payment": "$$ROOT" } } }, { $unionWith: { coll: "refund", pipeline: [ { $replaceRoot: { newRoot: { "_id": "$_id", "type": "refund", "refund": "$$ROOT" } } } ]} } ])Este script funciona perfectamente.
mi codigo java
List<AggregationOperation> list = new ArrayList<>(); ReplaceRootOperation replecePayment = replaceRoot() .withValueOf( ObjectOperators.valueOf("payment").mergeWith(Aggregation.ROOT) ); UnionWithOperation unionWithOperation = UnionWithOperation .unionWith("refund") .pipeline( replaceRoot().withValueOf( ObjectOperators.valueOf("refund").mergeWith(Aggregation.ROOT) ) ); LimitOperation limitOperation = Aggregation.limit(pageSize); list.add(replecePayment); list.add(unionWithOperation); list.add(limitOperation); Aggregation aggregation = newAggregation(Transaction.class, list); Flux<Transaction> output = mongoTemplate .aggregate(aggregation, "payment", Transaction.class); Transaction transaction = output.blockFirst();El objeto no me regresa como esperaba, creo que el problema es ReplaceRootOperation. No encontré ningún ejemplo similar con lo que necesito para mi caso, así que lo implementé de esa manera solo con fines de prueba.
Mi resultado en este momento
Transaction(id=359fd8a7-2228-40f1-95dc-e52554f5df21, type=null, payment=null, refund=null)