kumaranc
kumaranc

Reputation: 105

How to Get data from two collections for a request using Apache Camel

I'm new to apache camel. Am using mongo component in apache camel. In that i have two collections(collection1 and collection2), Before i post a data in collection1, i need to get qty from collection2. By combining both collection1 request and collection2, the document should create. I've done with single collection, but i'm struggling with two collections. Kindly guide someone to solve this. Below is the source code for it,

rest().post("/createcollection1")
    .route()
    .log("${body}")
    .to("mongodb:myMongo?database=practice&collection=collection1&operation=insert");

sample response:

     {
        "_id" : "id",
        "item" : "item",
        "cost" : 45,
        "status" : "available",
        "qty": 50
     }

Upvotes: 0

Views: 286

Answers (1)

You actually need a Content Enricher (EIP).

One way of solving this...

Before .to, you can declare:

.enrich("mongodb//<query-collection2>", new AggregationStrategy())

Then in the aggregation strategy you extract whatever you want from the search result (new exchange) and aggregate that to the going-through message (current exchange).

This doc will help you for sure: Content Enricher

Tip: Understanding Enterprise Integration Patterns is essential to properly use Camel.

Upvotes: 1

Related Questions