Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

304
Visualizações
How do I turn my MongoDB aggregation script into a java spring webflux code for use with mongoTemplete?

The idea is to unite the payment and refund colletions, transform it into a new collection called transaction, which will have the result of this union.

My script

db.payment.aggregate([
    { $replaceRoot: { newRoot: { "_id": "$_id", "type": "payment", "payment": "$$ROOT" } } },
    { $unionWith: { coll: "refund", pipeline: [ { $replaceRoot: { newRoot: { "_id": "$_id", "type": "refund", "refund": "$$ROOT"  } } } ]} }
])

This script is working perfectly

My java code

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();

The object does not return to me as I expect, I believe the problem is ReplaceRootOperation. I didn't find any similar example with what I need for my case, so I implemented it that way for testing purposes only.

My result at the moment

Transaction(id=359fd8a7-2228-40f1-95dc-e52554f5df21, type=null, payment=null, refund=null)
over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda