How to listen for changes to a MongoDB collection?

后端 未结 11 1642
不思量自难忘°
不思量自难忘° 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:54

    Many of these answers will only give you new records and not updates and/or are extremely ineffecient

    The only reliable, performant way to do this is to create a tailable cursor on local db: oplog.rs collection to get ALL changes to MongoDB and do with it what you will. (MongoDB even does this internally more or less to support replication!)

    Explanation of what the oplog contains: https://www.compose.com/articles/the-mongodb-oplog-and-node-js/

    Example of a Node.js library that provides an API around what is available to be done with the oplog: https://github.com/cayasso/mongo-oplog

提交回复
热议问题