MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

How to use change stream in java

I want to run a function whenever a new document is added to my collection. But for each is not applicable in the watch method. How to use change stream properly in java android.

Please help.

What is a RemoteMongoCollection? Is that a library you wrote or is it from a third party?

I am not too sure but I think you block needs to be between () not after.

You have db.watch().forEach() { } but what you need is db.watch().forEach( { } ). And your {} must implements Block< ChangeStreamDocument< Document > > and as such you must have a method public void apply( final ChangeStreamDocument change ).

I removed the changed it to MongoCollection and the forEach() was working, but I was not able to initialize the MongoCollection (ie. db).


Then I changed RemoteMongoClient to MongoClient, but then I was unable to initialize that also.
All the screenshots are below as I can’t upload more that one image in the forum.

https://drive.google.com/drive/folders/1aLWHo6WwBacMVi6GLb90fFujuHYWncGx?usp=sharing
Please help.

I am probably using “Remote” because I am using Stitch of MongoDB