Autoupdate document field kmongo

I’m using KMongo, and I’m looking for a way to automatically update documents inside mongo db, every time a document is touched/retrieved/updated/… I was wondering if there is some built in functionallity or some specific way to do this. Thank you in advance!

Hi @Violeta_Costea,

Kmongo is a 3rd party project for kotlin. The java driver support change streams which is probably what you are looking for.

https://docs.mongodb.com/manual/changeStreams/
I am not familiar with kmongo but if its based on the mongo java driver it might have this capability…
https://mongodb.github.io/mongo-java-driver/4.1/driver-reactive/tutorials/change-streams/

If you can use Atlas as your deployment you can use build in trigger mechanism which is client agnostic:
https://docs.atlas.mongodb.com/triggers/index.html

Hope this helps.

Best
Pavel

Hey Pavel,

Thank you for your response, it was really useful :slight_smile:

Kotlin is based on java and there is a function called watch(), that can listen to any changes that go to a db/colleection/deployment, same as shown in the java driver documentation from above.

There is a thing that i couldn’t figure out, so while using a change stream, does it act as a middleware? and can I actually interfere with the mongo operation? or is this change stream acting only as a listener that returns all activity that already happened ( for example if I insert a document, will the change stream show me this activity after it was inserted in db or before it was actually inserted? )

Hope this makes sense

Thank you!

Hi @Violeta_Costea,

The change stream listen to the oplog collection and. Filter the needed events after they happened. It cannot block ot interfere with any of the applied operations.

This is why the oplog size and resume data are so coupled as the changestream seek for ita information there.

Best
Pavel

Thank you very much @Pavel_Duchovny! I have a much clearer understanding on how the change stream works now :slight_smile:

1 Like