【问题标题】:Why mongodb return empty entities from change streams?为什么 mongodb 从更改流中返回空实体?
【发布时间】:2021-03-19 16:19:30
【问题描述】:

我在 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)
    }

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

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

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

【问题讨论】:

    标签: mongodb kotlin project-reactor changestream


    【解决方案1】:

    我使用了另一个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-14
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 2022-06-28
      相关资源
      最近更新 更多