Lazy loading using nodejs and mongoDB as backend d

2019-07-20 18:10发布

问题:

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

回答1:

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


回答2:

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


回答3:

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