使用nodejs和mongoDB作为后端数据的延迟加载 [英] Lazy loading using nodejs and mongoDB as backend data
问题描述
我们的收藏大约有1亿份文档.我们在mongo查询中使用带有限制子句的nodejs和expressjs创建了一个简单的应用程序.到目前为止,对于用户而言已经足够.同时,我们正在尝试实现延迟加载,以使初始页面加载返回的文档很少,而当用户滚动时,我们希望进一步加载文档.努力从哪里开始以及如何继续实施它.感谢您的建议. 我的index.js文件看起来像这样
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
推荐答案
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
});
});
});
这篇关于使用nodejs和mongoDB作为后端数据的延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!