我有 mongodb 和 NodeJs。连接通过 mongoosejs 完成.
开发ajax无限滚动的最佳方法是什么?我应该使用限制和偏移吗?
最佳答案
当您对数据集进行分页时,“跳过和限制”方法效率不高。它实际上是 Shlemiel the Painter's algorithm .
范围查询效率更高(当索引支持时)。例如,假设您正在显示推文。您的页面大小为 20,并且您在第 1000 页上并且想要加载第 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
上有索引)。
你明白了:当你加载一个页面时,记下最后一条推文的时间戳,并用它来查询下一页。
关于ajax - 使用 MongoDb 和 Nodejs 进行 ajax 分页的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10072518/