angryip
angryip

Reputation: 2290

Mongodb $set and $inc in camel

I am attempting to use the $set and $inc flags to update a field and increment a field by 1 in camel ( java ). Here is the code I am using:

from("direct:mySampleRoute").routeId("update-mongo-db-entry").setBody()
    .simple("{\"person\": \"${headers.personName}\", \"job\": \"computerGuy\",\"title\": \"DR\"},"
    + "{\"$set\": {\"pay\": \"${headers.newPay}\"}, \"$inc\": {\"hrPay\": 1}}")
    .toD("mongodb:mongoClient?database=" + mongoDb + "&collection="
            + mongoEmplyeeCollection + "&operation=update")
    .end();

The goal of the query is to find an entry where person == ${headers.personName}, job == computerGuy, and title = DR. Once it is found, it is to set the pay field to ${headers.newPay} and inc the hrPay field by positive 1. From what I can tell, I am structuring my code exactly the same as mentioned in this post: What am I doing wrong with $set and $inc in update However, once executed, my app crashes and I am unable to see any logs wrt to why that query fails. As such, I suspect I have a fundamental problem in structuring the json update. Camel documentation located here is of limited help.

Using camel, how can I achieve my goal of making a "select" query, updating some fields using $set, and incrementing a field using $inc ?

Upvotes: 0

Views: 545

Answers (1)

angryip
angryip

Reputation: 2290

I ended up solving my problem using a Processor to create the needed body. I slved it using this syntext within the process method:

@Override
public void process(final Exchange exchange) throws Exception {
    final BasicDBObject filterQuery = new BasicDBObject();
    filterQuery.append("person", exchange.getIn().getHeader("personName"));
    filterQuery.append("job", "computerGuy");
    filterQuery.append("title", "DR");
    final BasicDBObject updateQuery = new BasicDBObject();
    updateQuery.append("$set", new BasicDBObject("pay", exchange.getIn().getHeader("newPay"))
            .append("location", "4th floor"));
    updateQuery.append("$inc", new BasicDBObject("hrPay", 1));
    final List<DBObject> query = new ArrayList<>();
    query.add(filterQuery);
    query.add(updateQuery);
    exchange.getIn().setBody(query);
}

Upvotes: 1

Related Questions