3

Our collection has around 100 million documents. We created a simple application using nodejs and expressjs with a limit clause in the mongo query . It is sufficient for the users as of now. In the mean time we are trying to implement lazy loading so that the initial page load returns few documents and when the user scrolls we would like to load further documents. Struggling where to start and how to ahead to implement it. Appreciate your suggestions. My index.js file would look like this

router.get('/users', function(req, res) {
  var db = req.db; 
  var users = db.get('users'); 

  users.find(query, {limit: 10000}, function(e, docs){
    res.render('users', { 
      title: 'Users',
      'users': docs  
    });
  });
});

I would like to remove the limit and using skip trying to achieve this. Please post your suggestions

3 Answers 3

7

This should help. It uses the .skip method of the .find() cursor. I call it pagination rather than lazy loading.

var itemsPerPage = 10;

router.get('/users/:pageNum', function(req, res) {
  var db = req.db; 
  var users = db.get('users'); 
  users.find(query, {skip: (itemsPerPage * (pageNum-1)), limit: itemsPerPage},function(e, docs){
    res.render('users', { 
      title: 'Users',
      'users': docs  
    });
  });
});
Sign up to request clarification or add additional context in comments.

4 Comments

so from where pageNum should be passed , If we do it as pagination ?
That will be part of the request you make. http://server.tld/users/2. I haven't used expressjs a lot, so I'm not sure if that is how you define query params.
I also agree with @bidhan-a's suggestion to use Mongoosejs.
Thanks very much . I did it with javascript with 2 buttons namely previous and next. it works
0

You could implement paging in your route

Something like below;

// GET /users?start=20
router.get('/users', function(req, res) {
  var db = req.db; 

  var start = parseInt(req.query.start) || 0;
  var users = db.get('users'); 

  users.find(query, {start: start, limit: 10000}, function(e, docs){
    res.render('users', { 
      title: 'Users',
      'users': docs  
    });
  });
});

Comments

-2

I suggest you use Mongoose to model the MongoDB objects efficiently and then make use of one of many mongoose pagination modules available.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.