How to listen for changes to a MongoDB collection?

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

    Check out this: Change Streams

    January 10, 2018 - Release 3.6

    *EDIT: I wrote an article about how to do this https://medium.com/riow/mongodb-data-collection-change-85b63d96ff76

    https://docs.mongodb.com/v3.6/changeStreams/


    It's new in mongodb 3.6 https://docs.mongodb.com/manual/release-notes/3.6/ 2018/01/10

    $ mongod --version
    db version v3.6.2
    

    In order to use changeStreams the database must be a Replication Set

    More about Replication Sets: https://docs.mongodb.com/manual/replication/

    Your Database will be a "Standalone" by default.

    How to Convert a Standalone to a Replica Set: https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/


    The following example is a practical application for how you might use this.
    * Specifically for Node.

    /* file.js */
    'use strict'
    
    
    module.exports = function (
        app,
        io,
        User // Collection Name
    ) {
        // SET WATCH ON COLLECTION 
        const changeStream = User.watch();  
    
        // Socket Connection  
        io.on('connection', function (socket) {
            console.log('Connection!');
    
            // USERS - Change
            changeStream.on('change', function(change) {
                console.log('COLLECTION CHANGED');
    
                User.find({}, (err, data) => {
                    if (err) throw err;
    
                    if (data) {
                        // RESEND ALL USERS
                        socket.emit('users', data);
                    }
                });
            });
        });
    };
    /* END - file.js */
    

    Useful links:
    https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set
    https://docs.mongodb.com/manual/tutorial/change-streams-example

    https://docs.mongodb.com/v3.6/tutorial/change-streams-example
    http://plusnconsulting.com/post/MongoDB-Change-Streams

提交回复
热议问题