Why mongodb return empty entities from change streams?

402 Views Asked by At

I have a collection in mongodb which is watched with 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)
    }

The events I watch for are: "insert, update, replace".

The events are received in function, but the entities include all null fields (or default values for primitives for example).

Is there a way to get the object I create or update from changestreams?

1

There are 1 best solutions below

0
On BEST ANSWER

I used another overload of watch method with class as a parameter and it worked:

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