We had this following query in our Node.JS
我们在Node.JS中有以下查询
mongo.connect('mongodb://XXX:XXX@ds054XXX.mlab.com:54289/XXXdb', function (err, db) {
var collection = db.collection('chatmessages')
var stream = collection.find().sort({ _id: -1 }).limit(20).stream();
stream.on('data', function (chat) {
socket.emit('user message', chat.content, chat.dateCreated);
});
});
As you can see, the query is a collection of last 20 records entered. But then from this result we would like to do resort again to 1 in _id so in the list we will have ID 55 - 75 for instance (the order). So the last one always at the bottom.
如您所见,查询是输入的最后20条记录的集合。但是从这个结果我们想再次使用_id中的1,所以在列表中我们将得到ID 55 - 75(例如)。所以最后一个总是在底部。
How do we achieve this again?
我们如何再次实现这一目标?
2 个解决方案
#1
5
You need to use the Aggregation Framework.
您需要使用聚合框架。
mongo.connect('mongodb://XXX:XXX@ds054XXX.mlab.com:54289/XXXdb', function (err, db) {
var collection = db.collection('chatmessages')
var stream = collection.aggregate([
{ "$sort": { "_id": -1}},
{ "$limit": 20 },
{ "$sort": { "_id": 1 }}
]);
stream.on('data', function (chat) {
socket.emit('user message', chat.content, chat.dateCreated);
});
});
#2
2
The simplest thing to do will be to reverse the order of records returned by the query. So, instead of getting a stream, convert the response to array and use reverse()
function, to reverse the order of records in it, before sending it through the socket!
最简单的方法是颠倒查询返回的记录顺序。因此,在通过套接字发送之前,不是获取流,而是将响应转换为数组并使用reverse()函数来反转其中的记录顺序!
mongo.connect('mongodb://XXX:XXX@ds054XXX.mlab.com:54289/XXXdb', function (err, db) {
var collection = db.collection('chatmessages')
collection.find().sort({ _id: _1 }).limit(20).toArray(function(err, docs) {
if (err) {
// handle error
}
docs.reverse(); // <-- This will reverse the order of records returned!
// Send the the array of records through socket.
socket.emit('user message', docs)
})
});
#1
5
You need to use the Aggregation Framework.
您需要使用聚合框架。
mongo.connect('mongodb://XXX:XXX@ds054XXX.mlab.com:54289/XXXdb', function (err, db) {
var collection = db.collection('chatmessages')
var stream = collection.aggregate([
{ "$sort": { "_id": -1}},
{ "$limit": 20 },
{ "$sort": { "_id": 1 }}
]);
stream.on('data', function (chat) {
socket.emit('user message', chat.content, chat.dateCreated);
});
});
#2
2
The simplest thing to do will be to reverse the order of records returned by the query. So, instead of getting a stream, convert the response to array and use reverse()
function, to reverse the order of records in it, before sending it through the socket!
最简单的方法是颠倒查询返回的记录顺序。因此,在通过套接字发送之前,不是获取流,而是将响应转换为数组并使用reverse()函数来反转其中的记录顺序!
mongo.connect('mongodb://XXX:XXX@ds054XXX.mlab.com:54289/XXXdb', function (err, db) {
var collection = db.collection('chatmessages')
collection.find().sort({ _id: _1 }).limit(20).toArray(function(err, docs) {
if (err) {
// handle error
}
docs.reverse(); // <-- This will reverse the order of records returned!
// Send the the array of records through socket.
socket.emit('user message', docs)
})
});