how to close mongodb tailable cursor stream

68 views
Skip to first unread message

arian

unread,
Nov 12, 2014, 1:24:06 AM11/12/14
to mongod...@googlegroups.com
I use the below code to get the data from mongodb capped collection

function listen(conditions, callback) {

            db.openConnectionsNew( [req.session.client_config.db] , function(err, conn){

                if(err) {console.log({err:err}); return next(err);}

                coll = db.opened[db_name].collection('lg_seconds');

            latestCursor = coll.find(conditions).sort({$natural: -1}).limit(1)
            latestCursor.nextObject(function(err, latest) {
                if (latest) {
                    conditions._id = {$gt: latest._id}
                }
                options = {
                    tailable: true,
                    awaitdata: true,
                    numberOfRetries: -1
                }
                stream = coll.find(conditions, options).sort({$natural: -1}).stream()
                stream.on('data', callback)
            });

            });
        }

and then I use sockets.broadcast(roomName,'data',document);  

but i have been not able to figure out :( how I can close this stream when socket connection is closed...



arian

unread,
Nov 13, 2014, 5:03:52 AM11/13/14
to mongod...@googlegroups.com
I asked the same question over stackoverflow and got the answer that I could use stream.pause to stop the streaming query,
but the problem is it only works when any data is being received from server, is there anyway to stop the query which starts when stream() is called ?
Reply all
Reply to author
Forward
0 new messages