什么是做阿贾克斯分页与MongoDB的和Nodejs的最佳方法是什么? [英] What is the best way to do ajax pagination with MongoDb and Nodejs?

查看:156
本文介绍了什么是做阿贾克斯分页与MongoDB的和Nodejs的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MongoDB和NodeJs。的连接是通过 mongoosejs 的完成。
什么是开发Ajax无限滚动的最好方法?我应该使用的限制和偏移?

I have mongodb and NodeJs. The connection is done via mongoosejs.
What is the best way to develop ajax infinity scroll ? Should I use limit and offset ?

推荐答案

跳过和限制的做法是不是很有效,当你分页远到数据集。这是一个有效 Shlemiel画家的算法

"skip and limit" approach is not very efficient when you are paging far into dataset. It is efficiently a Shlemiel the Painter's algorithm.

范围查询是更有效的(当由索引支持)。例如,让我们想象一下,你在显示鸣叫。您的页面大小为20,你是1000页,并要加载1001页。

Range queries are much more efficient (when supported by indexes). For example, let's imagine that you're displaying tweets. Your page size is 20 and you're on page 1000 and want to load page 1001.

此查询

db.tweets.find().sort({created_at: -1}).skip(1001*20).limit(20)

db.tweets.find({created_at: {$lt: last_displayed_date}}).
          sort({created_at: -1}).limit(20);

(前提是你得指数 created_at )。

您的想法:当你下载一个页面,注意最后的鸣叫的时间戳记,并用它来查询下一个页面

You get the idea: when you load a page, note the timestamp of the last tweet and use it to query the next page.

这篇关于什么是做阿贾克斯分页与MongoDB的和Nodejs的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆