Reputation: 17589
Is there a way to listen to a MongoDB collection and have a callback gets triggered when a collection has a new document?
Upvotes: 3
Views: 715
Reputation: 230326
There aren't any active pushes from the DB, but you could hook into replication.
Let's assume you have a replica set (you wouldn't run single mongod, would you?).
Every change is written to the oplog on the primary and then is replicated to secondaries.
You can efficiently pull new changes (both inserts and updates) from the oplog, using tailable cursors. Note, this is still pull, not push.
Upvotes: 1
Reputation: 33155
Looks like there isn't a way yet. There is a lot of discussion in the "triggers" JIRA about related topics: https://jira.mongodb.org/browse/SERVER-124
You can work around this by polling with timestamps or counts, but an event callback would obviously be better.
Upvotes: 1