How to see any changes (new row) in mongoDB?

2019-02-19 09:56发布

问题:

Is there any way to observe each collection (or even one) in mongoDB? Now I think about timer to check document number or last Id, but maybe there is some possibility to implement mechanism like newDocumentAddedEvent?

回答1:

There are no triggers in MongoDB (yet?), but if you're running a replica set (as you should), your app can pretend to be a catching-up secondary, tail the oplog collection and get information about new inserts/updates.

This is a very efficient approach (mongodb itself uses it for the replication).