我想从 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/