【问题标题】:NodeJS MongoDB Recurring Tailable CursorNodeJS MongoDB 循环尾游标
【发布时间】:2016-06-18 18:01:06
【问题描述】:

我正在尝试使用可尾光标流设置 websocket 服务器。我有一个上限集合,到目前为止,我已经尝试使用 mongo 3.2 和 mongo 2.6 来做到这一点。

这是我一直在努力的工作。它将运行一次并获取所有文档,但不会获取我插入的新文档。我已经在无数示例中看到过此代码,并且看到有人流式传输聊天服务器,但似乎无法让它为我自己工作。

function startStream(){
const stream = collections.MQS.find({}, {tailable:true, awaitdata:true, numberOfRetries:-1}).stream();

stream.on('data', (doc)=>{
   console.log(doc); 
});    
}

我在连接中调用这个函数:

Mongo.MongoClient.connect (mongodbUri, onConnected);
function onConnected(err, database){
if (err) {throw err;}

// Assign db and collection letiables
collections.db = database;
collections.MQS = database.collection('mqs');

console.log("Connected to: " + mongodbUri);
startStream();
}

有没有我找不到的更合适的方法来做到这一点?

如果这个查询必须是递归的,那么在正常的查找查询中使用可尾选项与管理时间戳有何意义?

【问题讨论】:

  • 您的代码实际上对我有用。你只需要在一个有上限的集合上运行它。

标签: node.js mongodb


【解决方案1】:

您似乎没有对流进行排序,所以我猜您只是在跟踪集合的开始:除非达到集合的上限,否则这不会有太大变化。

尝试在流中添加自然排序:

collections.MQS.find({}, {tailable:true, awaitdata:true, numberOfRetries:-1}).sort({$natural: -1}).stream();

文档中的最新语法:

collections.MQS.find({}).addCursorFlag('tailable', true).stream();

http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#stream

【讨论】:

  • 感谢您的评论。当我将排序添加到承诺链时,我得到了错误:“MongoError:Tailable 光标不支持排序”
  • 使用的MongoDB版本和驱动版本是多少? @唐
  • 我正在使用驱动程序版本 2.1.7,并且同时使用了 mongodb 2.6 和 3.2。在这一点上,我已经开始转向 AMQP 和 RabbitMQ,因为这个解决方案似乎不起作用。
【解决方案2】:

Here是一个例子,展示了如何通过cursor为新的MongoDB文档设置subscriber函数,并在控制台打印新文档。

/**
 * How to subscribe for new MongoDB documents in Node.js using tailable cursor
 */

// subscriber function
var subscribe = function(){  
  var args = [].slice.call(arguments);
  var next = args.pop();
  var filter = args.shift() || {};

  if('function' !== typeof next) throw('Callback function not defined');

 // connect to MongoDB
  require('mongodb').MongoClient.connect('mongodb://localhost/test', function(err, db){

   // make sure you have created capped collection "messages" on db "test"
    db.collection('messages', function(err, coll) {

     // seek to latest object
      var seekCursor = coll.find(filter).sort({$natural: -1}).limit(1);
      seekCursor.nextObject(function(err, latest) {
        if (latest) {
          filter._id = { $gt: latest._id }
        }

       // set MongoDB cursor options
        var cursorOptions = {
          tailable: true,
          awaitdata: true,
          numberOfRetries: -1
        };

       // create stream and listen
        var stream = coll.find(filter, cursorOptions).sort({$natural: -1}).stream();

       // call the callback
        stream.on('data', next);
      });
    });   
  });     
};

// new documents will appear in the console
subscribe( function(document) {
  console.log(document);  
});

【讨论】:

  • 我在想之前就遇到了这个例子。下一个回调对我来说有点模棱两可,但我认为这是在做同样的事情,除了初始光标寻求最后一项设置为过滤器。它似乎也有同样的错误:“MongoError: Tailable cursor does not support sorting”,作为乔纳森的解决方案。
  • @Don,根据.sort source code,请尝试将tailable 更改为false
  • @Don,nextnext = args.pop();,这是传入的 function(document){console.log();} 函数...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
  • 2018-05-15
相关资源
最近更新 更多