简体   繁体   English

在nodejs中Mongodb Tailable Cursor,如何停止流

[英]Mongodb Tailable Cursor in nodejs, how to stop stream

I use below code to get the data from mongodb capped collection 我使用下面的代码从mongodb capped集合中获取数据

function listen(conditions, callback) { 函数listen(条件,回调){

        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('messages');

        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); 然后我使用sockets.broadcast(roomName,'data',document);

on client side 在客户端

io.socket.get('/get_messages/', function(resp){
});
io.socket.on('data', function notificationReceivedFromServer ( data ) {
  console.log(data);

}); });

this works perfectly as I am able to see the any new document which is inserted in db. 这非常有效,因为我能够看到插入db中的任何新文档。

I can see in mongod -verbose that after each 200ms there is query running with the query {$gt:latest_id} and this is fine, but I have no idea how can i close this query :( I am very new in nodejs and using the mongodb tailable option for the first time and am totally lost, any help or clue is highly appreciated 我可以在mongod -verbose中看到,在每个200ms之后有查询运行查询{$ gt:latest_id},这很好,但我不知道如何关闭此查询:(我在nodejs和使用中非常新第一次mongodb tailable选项,我完全迷失了,任何帮助或线索都非常感谢

What is returned from the .stream() method from the Cursor object returned from .find() is an implementation of the node transform stream interface. 什么是从返回.stream()从方法光标对象从返回.find()是所述的一种实现节点变换流接口。 Specifically this is a "readable" stream. 具体来说,这是一个“可读”的流。

As such, it's "data" event is emitted whenever there is new data received and available in the stream to be read. 因此,只要在要读取的流中接收到新数据并且可用,就会发出“数据”事件。

There are other methods such as .pause() and .resume() which can be used to control the flow of these events. 还有其他方法,如.pause().resume() ,可用于控制这些事件的流程。 Typically you would call these "inside" a "data" event callback, where you wanted to make sure the code in that callback was executed before the "next" data event was processed: 通常,您将这些“内部”称为“数据”事件回调,您希望确保在处理“下一个”数据事件之前执行该回调中的代码:

stream.on("data", function(data) {
   // pause before processing
   stream.pause();

  // do some work, possibly with a callback
  something(function(err,result) {

      // Then resume when done
      stream.resume();
  });
});

But of course this is just a matter of "scoping". 但当然这只是“范围界定”的问题。 So as long as the "stream" variable is defined in a scope where another piece of code can access it, then you can call either method at any time. 因此,只要在另一段代码可以访问它的范围内定义“stream”变量,就可以随时调用任一方法。

Again, by the same token of scoping, you can just "undefine" the "stream" object at any point in the code, making the "event processing" redundant. 同样,通过相同的范围标记,您可以在代码中的任何位置“取消定义”“流”对象,使“事件处理”变得多余。

// Just overwrite the object
scope = undefined;

So worth knowing. 所以值得了解。 In fact the newer "version 2.x" of the node driver wraps a "stream interface" directly into the standard Cursor object without the need to call .stream() to convert. 实际上,节点驱动程序的较新“版本2.x”将“流接口”直接包装到标准Cursor对象中,而无需调用.stream()进行转换。 Node streams are very useful and powerful things that it would be well worth while coming to terms with their usage. 节点流是非常有用和强大的东西,在与它们的使用达成协议时非常值得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM