229.7.2. 更改流消费者
通过更改流,应用程序无需跟踪 MongoDB oplog,即可访问实时数据变化。应用程序可以使用更改流来订阅集合上的所有数据更改,并立即响应它们。由于更改流使用聚合框架,所以应用程序也可以过滤特定更改,或者转换位于 的通知。
要配置 Change Streams Consumer,您需要指定 consumerType
、数据库
、集合和
可选的 JSON 属性 streamFilter
来过滤事件。该 JSON 属性是标准的 MongoDB $match
聚合。可使用 XML DSL 配置轻松指定:
<route id="filterConsumer"> <from uri="mongodb3:myDb?consumerType=changeStreams&database=flights&collection=tickets"/> <to uri="mock:test"/> <routeProperty key="streamFilter" value="{'$match':{'$or':[{'fullDocument.stringValue': 'specificValue'}]}}"/> </route>
Java 配置:
from("mongodb3:myDb?consumerType=changeStreams&database=flights&collection=tickets") .routeProperty("streamFilter", "{'$match':{'$or':[{'fullDocument.stringValue': 'specificValue'}]}}") .to("mock:test");