Mongoose stream returns multiple results for the first time

I am working on a new live news service, I have a problem right now that I don't know how to solve.

First of all, when a user connects to the NodeJS server, I create a Mongoose stream, so I can return this data easily and quickly.

The problem I am having now is to return only a few datasets the first time, and the following code returns the entire collection:

io.sockets.on('connection', function(socket) {
    console.log("New user has been connected");

    var stream = News.find().tailable().stream();

    stream.on('error', function (err) {
      console.error(err)
    });

    stream.on('data', function (doc) {
      socket.emit("newArticle", doc);
    }); 
}); 

      

So the question is, how ... can I return only the last ten results the first time?

+1


source to share


2 answers


Have you tried limiting your request? As shown in mongoose.js file in requests .

With your code, it would be



io.sockets.on('connection', function(socket) {
  console.log("New user has been connected");

  var stream = News.find().tailable().limit(10).stream();

  stream.on('error', function (err) {
    console.error(err)
  });

  stream.on('data', function (doc) {
    socket.emit("newArticle", doc);
  });
});

      

0


source


Finally, I do this by limiting the request from the current time minus half an hour, so the first time I just get a small set of data.



0


source







All Articles