java - 如何在 spring 中使用 change stream 监听多个集合

标签 java mongodb spring-boot

我想从 collection_one、collection_two、collection_three 等收集数据,我该怎么做?

       ChangeStreamRequest request = ChangeStreamRequest.builder().collation(Collation.of("collection_one"))
            .filter(Aggregation.newAggregation(match(where("operationType").exists(true))))
            .publishTo(krakenDtoMessageListener)
            .build();
    container.register(request, CollectionOne.class);

我应该创建多个 changeStreamRequest 还是一个应该没问题?

最佳答案

配置更改流时,您可以指定集合过滤器。

检查下面的java代码,

List<Bson> pipeline = singletonList(Aggregates.match(
    Filters.in("ns.coll", asList("coll1", "coll2", "coll3"))
));

MongoCursor<ChangeStreamDocument<Document>> cursor = db.watch(pipeline).fullDocument(FullDocument.UPDATE_LOOKUP).iterator();

您可以使用 spring 框架编写类似的管道。

关于java - 如何在 spring 中使用 change stream 监听多个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58240969/

相关文章:

java - 使用 JavaFX 时出现 NullPointerException

java - 为什么我的自定义 GridView 仅显示一列?

mongodb - Sails-Mongo 对特定集合的 native 查询

postgresql - SpringbootTest + TestContainers : how do I refresh the database after tests pollute the database

java - REST放心,如何从JsonPath类静态导入 "from"?

Java 6 : how to pass multiple parameters to APT

java - glassfish 如何使用 ManagedObjectManagers?

sql - RDBMS 或 NoSQL 用于具有循环和有序操作的复杂计划数据?

node.js - 从动态模式获取数据

java - Spring 配置附加位置作为 docker-compose 中的环境变量