0

How should i trigger mongodb event for instance trigger on Insert, Update or Delete to synchronize mysql DB. My requirement is to synchronize Mongodb to mysql using mongodb trigger, for instance if there is any insert, it should be replicate at mysql

  • Possible duplicate of [How to listen for changes to a MongoDB collection?](https://stackoverflow.com/questions/9691316/how-to-listen-for-changes-to-a-mongodb-collection) – HIRA THAKUR Feb 06 '19 at 05:50

1 Answers1

0

MongoDB does not support trigeers AFAIK.

However in case of replication they do maintain an oplog (short for operation log), it is used to sync operation from master to slave. In case if replication is enabled you can leverage oplog and query on that, it works like any other collection.

One more option is to use tailable cursor in capped collection. A tailable cursor is open even after it returns the result, so if a new result is returned, it sends the result to cursor. Though I think it won't work for update or delete cases. (https://docs.mongodb.com/manual/core/tailable-cursors/)

One custo approach is to do it on application level by scheduler which periodically do syncing or if you might use AOP.

Rahul Kumar
  • 2,781
  • 1
  • 21
  • 29
  • Hi, my requirement is for real time sync between mongodb and mysql at data layer. So whenever there is any insert, update or delete in mongob, it should trigger an event and replication/sync took place at mysql – Sarabjeet Singh Feb 06 '19 at 13:18
  • I dont want to do at application layer thats the big challenge – Sarabjeet Singh Feb 06 '19 at 13:19
  • unfortunately there is no way at database level to accomplish that however you can look at the link https://www.mongodb.com/blog/post/mongodb-and-data-streaming-implementing-a-mongodb-kafka-consumer , maybe you can get a reference to write your own connector However in comparison application level would be easier to implement in my opinion – Rahul Kumar Feb 07 '19 at 08:02