Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Nodejs Mongodb second sort()用于第一个sort()结果_Node.js_Mongodb - Fatal编程技术网

Node.js Nodejs Mongodb second sort()用于第一个sort()结果

Node.js Nodejs Mongodb second sort()用于第一个sort()结果,node.js,mongodb,Node.js,Mongodb,要显示的格式- msg - 9 ... msg - 17 msg - 18 msg - 19 从这个函数 mongo.connect(mongourl, function (err, db) { var collection = db.collection('chat') var stream = collection.find().sort({ _id : -1 }).limit(10).stream(); stream.on('data', function (chatt

要显示的格式-

msg - 9
...
msg - 17
msg - 18
msg - 19
从这个函数

mongo.connect(mongourl, function (err, db) {
   var collection = db.collection('chat')
   var stream = collection.find().sort({ _id : -1 }).limit(10).stream();
   stream.on('data', function (chatt) {clients[socket.id].emit('chat message', chatt.content); });     

});});
但这只会给我-

msg - 19
msg - 18
msg - 17
...
msg - 9
因为它是一个聊天应用程序,最新的行必须位于聊天格式的底部。 我尝试向结果中添加新的排序代码(我对node还不熟悉),它再次调用整个数据库并给出最早的10行

mongo.connect(mongourl, function (err, db) {
   var collection = db.collection('chat')
   var stream = collection.find().sort({ _id : -1 }).limit(10);
   stream=stream.sort({ _id : 1 }).stream();
   stream.on('data', function (chatt) {clients[socket.id].emit('chat message', chatt.content); });     

});});

如何从排序限制反转结果?非常感谢。

至少有两种方法可以做到这一点。公平警告:我没有在实际的MongoDB连接上测试这个。但是,这应该让校长们明白。愿原力与你一起踏上诺德之旅

仅使用数组:

mongo.connect(mongourl, function (err, db) {
  var collection = db.collection('chat');

  // Get the previous 10 initially.
  var prev10 = collection.find().sort({ _id : -1 }).limit(10).toArray();
  // flush it
  let msg = null;
  while (msg = prev10.pop()) {
    // Remove the last element, which is the first message sent, altering the array in place.
    // Send it to the socket
    client[socket.id].emit('chat_message', msg.content);
  }
});
对初始10使用数组,然后流:这有点麻烦,因为我们试图跳过初始数组获取返回的内容。我不想这么做,但是为什么不试试呢

首选,如果您只想使用streams,而不想麻烦初始查询:在这里,您可以返回10、20、100条最近的历史消息,并且它将始终根据您的需要进行调整。延迟总是确保在返回之前没有新记录要收集,按出现顺序为FILO(先进先出)。MongoDB本质上是让您的FIFO成为FILO,因为它的结果排序结构。因此,通过弹出它返回的数组来转换顺序

mongo.connect(mongourl, function (err, db) {
  var collection = db.collection('chat');
  let HISTORYCOUNT = 10;
  let DATADELAY = 10;
  let filoQue = [];
  let delayedFlush = null;
  // Set up the stream.
  var stream = collection.find().sort({ _id : -1 }).limit(HISTORYCOUNT).stream();
  // Read data.
  stream.on('data', function (chatt) {
    // New data show up quickly, so it won't flush...
    if (delayedFlush) { clearTimeout(delayedflush); }
    // ...but rather fill the FILO queue.
    filoQue.push(chatt);
    delayedFlush = setTimeout(function() {
      // Empty the first 10 in your preferred order 9 -> 19
      if (filoQue.length > 0) {
      let msg = null;
      while (msg = filoQue.pop();) {
        // Always remove the last element, which will be the first message sent, altering the array in place.
        // Send it to the socket
        client[socket.id].emit('chat_message', msg.content);
      }, DATADELAY);
   });
});

再一次,我警告说我没有在实际的MongoDB连接上测试它。只是为了确保排队符合您的意图而进行了测试。如果有bug,请告诉我,我可以编辑以帮助下一步。

至少有两种方法可以做到这一点。公平警告:我没有在实际的MongoDB连接上测试这个。但是,这应该让校长们明白。愿原力与你一起踏上诺德之旅

仅使用数组:

mongo.connect(mongourl, function (err, db) {
  var collection = db.collection('chat');

  // Get the previous 10 initially.
  var prev10 = collection.find().sort({ _id : -1 }).limit(10).toArray();
  // flush it
  let msg = null;
  while (msg = prev10.pop()) {
    // Remove the last element, which is the first message sent, altering the array in place.
    // Send it to the socket
    client[socket.id].emit('chat_message', msg.content);
  }
});
对初始10使用数组,然后流:这有点麻烦,因为我们试图跳过初始数组获取返回的内容。我不想这么做,但是为什么不试试呢

首选,如果您只想使用streams,而不想麻烦初始查询:在这里,您可以返回10、20、100条最近的历史消息,并且它将始终根据您的需要进行调整。延迟总是确保在返回之前没有新记录要收集,按出现顺序为FILO(先进先出)。MongoDB本质上是让您的FIFO成为FILO,因为它的结果排序结构。因此,通过弹出它返回的数组来转换顺序

mongo.connect(mongourl, function (err, db) {
  var collection = db.collection('chat');
  let HISTORYCOUNT = 10;
  let DATADELAY = 10;
  let filoQue = [];
  let delayedFlush = null;
  // Set up the stream.
  var stream = collection.find().sort({ _id : -1 }).limit(HISTORYCOUNT).stream();
  // Read data.
  stream.on('data', function (chatt) {
    // New data show up quickly, so it won't flush...
    if (delayedFlush) { clearTimeout(delayedflush); }
    // ...but rather fill the FILO queue.
    filoQue.push(chatt);
    delayedFlush = setTimeout(function() {
      // Empty the first 10 in your preferred order 9 -> 19
      if (filoQue.length > 0) {
      let msg = null;
      while (msg = filoQue.pop();) {
        // Always remove the last element, which will be the first message sent, altering the array in place.
        // Send it to the socket
        client[socket.id].emit('chat_message', msg.content);
      }, DATADELAY);
   });
});

再一次,我警告说我没有在实际的MongoDB连接上测试它。只是为了确保排队符合您的意图而进行了测试。如果有bug,请告诉我,我可以编辑以帮助下一步。

.sort({u id:1})
。表示“升序”,而不是使用
-1
执行的“降序”。当有疑问时。这就是它的目的。@NeilLunn,是的,你是对的,但我想得到的是最新的10行升序。哦,好的。然后最好使用“降序”排序,而不是“流”,使用
.toArray()
获取常规数组,然后将排序后的数组作为一个结果发送回聊天客户端。更简单且不浪费网络开销。您是否总是需要每条消息的最新10行,或是最新10行的一次初始提取,然后是一个实时流?@toszter,是的,最新10行的初始提取用于实时聊天。
.sort({u id:1})
。表示“升序”,而不是使用
-1
执行的“降序”。当有疑问时。这就是它的目的。@NeilLunn,是的,你是对的,但我想得到的是最新的10行升序。哦,好的。然后最好使用“降序”排序,而不是“流”,使用
.toArray()
获取常规数组,然后将排序后的数组作为一个结果发送回聊天客户端。更简单,不浪费网络开销。您是否总是需要每条消息的最新10行,还是需要对最新10行进行一次初始提取,然后实时获取剩余的流?@toszter,是的,对实时聊天的最新10行进行初始提取。是的,您的阵列解决方案可以解决此问题。非常感谢您的代码和参考资料。但我想提醒其他观众,不要像我的问题一样,对每个连接都使用到mongo db的直接连接。它使我的CPU占用了100%的流量。我通过mongoose express rest解决了这个问题。但我只是一个新的节点,所以这个建议可能是错误的。使用rest和数组是正确的。流确实是一个猪。是的,你的阵列解决方案可以解决这个问题。非常感谢您的代码和参考资料。但我想提醒其他观众,不要像我的问题一样,对每个连接都使用到mongo db的直接连接。它使我的CPU占用了100%的流量。我通过mongoose express rest解决了这个问题。但我只是一个新的节点,所以这个建议可能是错误的。使用rest和数组是正确的。这条小溪真是一头猪。