Skip to content

Latest commit

 

History

History
45 lines (32 loc) · 1.13 KB

README.md

File metadata and controls

45 lines (32 loc) · 1.13 KB

mongo-connect

mongo-connect supports using the MongoDB changestream.

Examples

create instance

MongoDatabase database = mongoClient.getDatabase("test-database");

MongoConnect mongoConnect = new MongoConnect();
// watch global for all changes
mongoConnect.open(mongoConnect.open(() -> monogClient.watch());
// watch only for database changes
mongoConnect.open(mongoConnect.open(() -> database.watch());
// watch only for collection changes
mongoConnect.open(mongoConnect.open(() -> database.getCollection("test-collection").watch());

listen for database events

mongoConnect.register(Event.DROP, (name) -> {
	System.out.println("database %s dropped".formatted(name));
});

listen for collection events

mongoConnect.register(Event.DROP, (databaseName, collectionName) -> {
	System.out.println("collection %s of database %s dropped".formatted(databaseName, collectionName));
});

listen for document events

mongoConnect.register(Event.INSERT, (databaseName, collectionName, document) -> {
	System.out.println("new document inserted into collection %s".format(collectionName));
});