javascript - Lazy Loading/More Data Scroll in Mongoose/Nodejs -
i wondering how implement lazy loading/more data on scroll mongoose. want load 10 posts @ time, i'm not sure how best load next 10 elements in query.
i have:
var q = post.find().sort("rating").limit(10); to load 10 posts highest "rating". how go doing next 10 posts?
the general concept of "paging" use .skip() "skips" on results have been retrieved, can this:
var q = post.find().sort( "rating" ).skip(10).limit(10); but really, can imagine going slow down considerably when few "pages" in. want smarter. "range query" want grab higher (or lower if descending ) results last set of results retrieved. given last value of 5 greater do:
var q = post.find({ "rating": { "$gt": 5 } }).sort( "rating" ).limit(10); looks okay, there still problem. if next "page" contained results rating of 5? query skip on , never display them.
the smart thing "keep" of _id values document since unique keys. apply same sort of thing, except time make sure not including results previous page in new one. $nin operator helps here:
var q = post.find({ "rating": { "$gte": 5 }, "_id": { "$nin": seenids } }) .sort( "rating" ).limit(10); whether seenids last page of results or more depends on "density" of value sorting on, , of course need "keep" these in session variable or something.
but try adapt this, range queries best performance result.
Comments
Post a Comment