VoodooChild92
VoodooChild92

Reputation: 2053

Observe the MongoDB and perform actions accordingly if the tables are updated

So, I'm developing an application in Rails with mongodb as the database. So, this mongodb serves as a common database for 3 other websites.

Now, my problem is I also have another MySQL database(I know, my data is spread everywhere!!) and whenever this mongodb database is updated, I also want to update the MySQL database. Basically, I want to observe for any changes in the mongodb database and reflect the same changes to the MySQL.

Show me a way to accomplish this. Thanks a ton. :)

Upvotes: 0

Views: 74

Answers (1)

VoodooChild92
VoodooChild92

Reputation: 2053

I found a precise solution for my need:

Using this gem called "MongoRiver": https://github.com/stripe/mongoriver

mongo = Mongo::MongoClient.from_uri(mongo_uri)
tailer = Mongoriver::Tailer.new([mongo], :existing)
outlet = YourOutlet.new(your_params)
stream = Mongoriver::Stream.new(tailer, outlet)
stream.run_forever(starting_timestamp)

In YourOutlet.rb, you define a class extending the Outlet class in the gem and define events like after_insert, after_update, before_insert, before_update etc. and they will be executed accordingly.

So, how this works is simple: As someone mentioned in the comments, you have to enable the Oplog in your MongoDB. And set a oplog size that's suitable to your need, usually of 1 GB.

Read about it here: http://docs.mongodb.org/manual/tutorial/deploy-replica-set/

Upvotes: 1

Related Questions