简体   繁体   English

使用 Apache Camel 从 Mongo DB 中删除文档

[英]Remove a document from Mongo DB with Apache Camel

My main issue might be not understanding some conventions in the Camel documents.我的主要问题可能是不理解 Camel 文档中的某些约定。

https://camel.apache.org/components/latest/mongodb-component.html#_delete_operations https://camel.apache.org/components/latest/mongodb-component.html#_delete_operations

They have a camel route commented out, and two Java objects being defined, which are not commented out.他们注释掉了一条骆驼路线,并定义了两个未注释掉的 Java 对象。 What are they trying to indicate?他们想表明什么? Where are these objects at in a project?这些对象在项目中的什么位置?

Anyway, I'm subscribed to a JMS queue that I have another camel route publishing to.无论如何,我订阅了另一个骆驼路线发布到的 JMS 队列。 The message is a JSON string, which I save to a Mongo DB.该消息是一个 JSON 字符串,我将其保存到 Mongo DB。 But what I'd like to do is remove any current documents (based on criteria) and replace it with the new message.但是我想做的是删除所有当前文档(基于标准)并将其替换为新消息。

from("jms:topic:orderbook.raw.feed")
.log("JMS Message: ${body}")
    .choice()
        .when().jsonpath("$.[?(@.type=='partial')]")
            // Figure out how to delete the old orderbook from Mongo with a type=T1
            .to("mongodb:mongo?database=k2_dev&collection=orderbooks&operation=save");

Does your orderbook have an ID?您的订单簿有 ID 吗? If so, you can enrich the JSON with an _id field (MongoDB default representation for identifiers) whose value would be that ID.如果是这样,您可以使用_id字段(标识符的 MongoDB 默认表示)来丰富 JSON,其值为该 ID。 Thus you'll be "upserting" that orderbook.因此,您将“更新”该订单簿。

Obs.: Sure the Camel docs could be better.观察:当然,Camel 文档可能会更好。

But if you really feel you'd have to perform a remove operation before saving an orderbook, another option would be to extract its type from the current JSON string and use it as a filter when removing.但是,如果您真的觉得在保存订单簿之前必须执行remove操作,另一种选择是从当前 JSON 字符串中提取其类型,并在删除时将其用作过滤器。 Something like:就像是:

from("jms:topic:orderbook.raw.feed")
    .log("JMS Message: ${body}")
    .filter("$.[?(@.type=='partial')]")
    .multicast().stopOnException()
        .to("direct://orderbook-removal")
        .to("direct://orderbook-save")
    .end()
;

from("direct://orderbook-removal")
    // extract type and set it as the body message. e.g. {"type":"T1"}
    .to("mongodb:mongo?database=k2_dev&collection=orderbooks&operation=remove")
;

from("direct://orderbook-save")
    .to("mongodb:mongo?database=k2_dev&collection=orderbooks&operation=save")
;

The multicast sends a copy of the message to each destination. multicast将消息的副本发送到每个目的地。 So the content won't be affected.所以内容不会受到影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM