widgg
widgg

Reputation: 1428

Java MongoDB pretend to be replication slave

What we're trying to do is what Meteor is doing with Mongo with LiveQuery, which is this:

Livequery can connect to the database, pretend to be a replication slave, and consume the replication log. Most databases support some form of replication so this is a widely applicable approach. This is the strategy that Livequery prefers with MongoDB, since MongoDB does not have triggers.

Source of that quote here

So is there a way with com.mongodb.*; in Java to create such replication slave so that it receives any notifications for each update that happens on the primary Mongo server?

Also, I don't see any replication log in the local database. Is there a way to turn them on?

If it's not possible to do it in Java, is it possible to create such solution in other languages (C++ or Node.js maybe)?

Upvotes: 1

Views: 375

Answers (1)

helmy
helmy

Reputation: 9507

You need to start your database with the --replSet rsName option, and then run rs.initiate(). After that you will see a rs.oplog collection in the local database.

What you are describing is commonly referred to as "tailing the oplog", which is based on using a Tailable Cursor on a capped collection (the MongoDB oplog in this case). The mechanics are relatively simple, there are numerous oplog tailing examples out there written in Java, here are a few:

Upvotes: 1

Related Questions