Nodejs Mongodb第二次sorting()第一次sorting()的结果

格式想要出现 –

msg - 9 ... msg - 17 msg - 18 msg - 19 

从这个function

 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); }); });}); 

但这种sorting只能给我 –

 msg - 19 msg - 18 msg - 17 ... msg - 9 

因为它是一个聊天应用程序和最新的行需要在聊天格式底部。 我尝试添加新的sorting代码(我是新来的节点),然后再次使用整个数据库,并给出最早的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); }); });}); 

我怎样才能扭转sorting的结果? 非常感谢。

至less有两种方法可以做到这一点。 公平警告:我没有在一个实际的MongoDB连接上testing这个。 但是,这应该得到校长。 愿你的旅程与你的力量与节点!

只使用一个数组:

 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,然后stream: 这是一个小KLUDGEY,因为我们试图跳过初始数组获取返回。 我宁愿不这样做,但嘿为什么不试一试呢?

 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; let count = 0; 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); // Increment count ++; } // Set up your stream, skipping the message already retrieved stream=collection.find({}).sort({ _id : -1 }).skip(count).stream(); stream.on('data', function (chatt) { // Stream each incoming message individually. clients[socket.id].emit('chat message', chatt.content); }); }); 

首选,如果你只想使用stream而不打扰最初的查询: 在这里,你可以返回10,20,100个最近的历史消息,它会随时调整你的需要。 延迟总是确保没有新的logging在按照出现的顺序返回之前收集,FILO(先入先出)。 MongoDB本质上是让你的FIFO成为一个FILO,因为它的结果sorting的结构。 所以你通过popup它返回的数组来转换顺序。

 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连接testing这个。 只是testing,以确保排队会为您的意图工作。 让我知道是否有错误,我可以编辑,以帮助下一个。