How to listen for changes to a MongoDB collection?

后端 未结 11 1693
不思量自难忘°
不思量自难忘° 2020-11-22 06:08

I\'m creating a sort of background job queue system with MongoDB as the data store. How can I \"listen\" for inserts to a MongoDB collection before spawning workers to proce

11条回答
  •  感动是毒
    2020-11-22 06:56

    What you are thinking of sounds a lot like triggers. MongoDB does not have any support for triggers, however some people have "rolled their own" using some tricks. The key here is the oplog.

    When you run MongoDB in a Replica Set, all of the MongoDB actions are logged to an operations log (known as the oplog). The oplog is basically just a running list of the modifications made to the data. Replicas Sets function by listening to changes on this oplog and then applying the changes locally.

    Does this sound familiar?

    I cannot detail the whole process here, it is several pages of documentation, but the tools you need are available.

    First some write-ups on the oplog - Brief description - Layout of the local collection (which contains the oplog)

    You will also want to leverage tailable cursors. These will provide you with a way to listen for changes instead of polling for them. Note that replication uses tailable cursors, so this is a supported feature.

提交回复
热议问题