0

我在 mongodb 中有一个通过 changestreams 监视的集合。

protected fun <DocTypeT : Any> startWatcher(collectionName: String, docTypeClass: KClass<DocTypeT>, operationTypes: List<MongoOperationType>, onCollectionChangeFunction: (DocTypeT) -> Unit) {
        val changeStreamPublisher = dbClient.getCollection(collectionName, docTypeClass.java)
            .watch(listOf(Aggregates.match(Filters.`in`("operationType", operationTypes.map { operationType -> operationType.value }.toList()))))
            .fullDocument(FullDocument.UPDATE_LOOKUP)

        changeStreamPublisher.withDocumentClass(docTypeClass.java).toFlux()
            .subscribe(onCollectionChangeFunction)
    }

我观察的事件是:“插入、更新、替换”。

事件在函数中接收,但实体包括所有空字段(或例如原语的默认值)。

有没有办法从变更流中获取我创建或更新的对象?

4

1 回答 1

0

watch我使用了另一个以类为参数的方法重载,它起作用了:

protected fun <DocTypeT : Any> startWatcher(collectionName: String, docTypeClass: KClass<DocTypeT>, operationTypes: List<MongoOperationType>, onCollectionChangeFunction: (DocTypeT) -> Unit) {
        val changeStreamPublisher = dbClient.getCollection(collectionName, docTypeClass.java)
            .watch(listOf(Aggregates.match(Filters.`in`("operationType", operationTypes.map { operationType -> operationType.value }.toList()))), docTypeClass.java)
            .fullDocument(FullDocument.UPDATE_LOOKUP)

        changeStreamPublisher.toFlux()
            .subscribe(onCollectionChangeFunction)
    }
于 2020-12-08T14:39:19.610 回答