denniss
denniss

Reputation: 17589

NodeJS and MongoDB: Is there a way to listen to a collection and have an callback be called when a collection has new document?

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

Answers (2)

Sergio Tulentsev
Sergio Tulentsev

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

Eve Freeman
Eve Freeman

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

Related Questions