Receive notification of modified documents in mongodb

I am trying to use mongodb as a network configuration storage. This same application runs on several computers on the network, each of them configures from the local mongodb. mongods are synchronized. I would like to receive a callback / notification in all n-1 applications if one application changes any of the configuration values. Is such a setting possible?

(This will save me from doing network transmission / synchronization, etc.)

+7
source share
3 answers

MongoDB does not yet have triggers, but you can attach your application to hide the oplog collection and do something every time the document is deleted (or updated, or inserted, etc.).

A message may appear on this blog about how to do this: http://www.kchodorow.com/blog/2010/10/12/replication-internals/

+9
source

What do you mean that mongods are synchronized? Do they really copy data between themselves? I assume that it is not as it seems that you want to control synchronization.

In the past, I have done something similar with MongoDB and asp, which requires a centralized instance of mongo (a couple of replicas, etc.). Basically, every time a local instance is changed, the limited collection in the central instance is also updated with the new version of the configuration value and the timestamp, when this value was last updated, and which server updated the value.

Then a separate separate thread is launched on separate servers, which holds the cursor with an open tail against the central instance. Whenever a new record is retrieved by the cursor, the new values ​​are compared with the timestamp of the local instance and updated accordingly (or not). Be careful when comparing these timestamps and the β€œauthoritative” server that made the changes to ensure that you have not completed the update storm. You also need to know if the update is because someone really changed the value or its value because the value was "replicated" - you do not want to update the central instance if the update is a replication update.

+2
source

Starting with mongodb 3.6, you can now hook actions into the change stream. This gives you a tail cursor that you can use to listen for changes (e.g. crud operations) in a particular collection.

A stream of changes is created on top of the popup and is accessible to everything that oplog uses. Thread changes are resumed and can also be used with aggregation operators like $ match, $ project ...

More details here (Java example): http://mongodb.imtqy.com/mongo-java-driver/3.6/driver/tutorials/change-streams/

And here is a snippet from https://www.mongodb.com/mongodb-3.6 (Java):

// 1. The database for reactive, real-time applications MongoClient mongoClient; // Create a new MongoClient with a MongoDB URI string. if (args.length == 0) { // Defaults to a localhost replicaset on ports: 27017, 27018, 27019 mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017,localhost:27018,localhost:27019")); } else { mongoClient = new MongoClient(new MongoClientURI(args[0])); } // Select the MongoDB database. MongoDatabase database = mongoClient.getDatabase("testChangeStreams"); database.drop(); sleep(); // Select the collection to query. MongoCollection<Document> collection = database.getCollection("documents"); // Create the change stream cursor. MongoCursor<Document> cursor = collection.watch().iterator(); 

If you are working in C #, examples can be found here :

  var inventory = database.GetCollection<BsonDocument>("inventory"); var document = new BsonDocument("x", 1); inventory.InsertOne(document); new Thread(() => { Thread.Sleep(TimeSpan.FromMilliseconds(100)); var filter = new BsonDocument("_id", document["_id"]); var update = "{ $set : { x : 2 } }"; inventory.UpdateOne(filter, update); }) .Start(); // Start Changestream Example 2 var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup }; var enumerator = inventory.Watch(options).ToEnumerable().GetEnumerator(); enumerator.MoveNext(); var next = enumerator.Current; enumerator.Dispose(); // End Changestream Example 2 var expectedFullDocument = document.Set("x", 2); next.FullDocument.Should().Be(expectedFullDocument); 
0
source

All Articles