我正在努力解決如何有效地分頁我的數據。如果我有一個網址,例如:example.com/items?page=5
,我怎樣才能每次檢索正確的記錄(這樣相同的記錄就不會顯示在另一頁上)?我想我必須首先對數據庫記錄進行排序,然後使用貓鼬進行範圍查詢,例如下面的查詢。Scalable Express + Mongoose排序和限制分頁
當記錄數量快速增長時,這種做法如何?我擔心分類過程將花費太長時間並且成爲瓶頸。有什麼建議?
Submission.find({
/* First Case: Hour */
created: { $lt: new Date(), $gt: new Date(year+','+month+','+day+','+hour+','+min+','+sec) } // Get results from start of current hour to current time.
/* Second Case: Day */
created: { $lt: new Date(), $gt: new Date(year+','+month+','+day) } // Get results from start of current day to current time.
/* Third Case: Month */
created: { $lt: new Date(), $gt: new Date(year+','+month) } // Get results from start of current month to current time.
/* Fourth Case: Year */
created: { $lt: new Date(), $gt: new Date(year) } // Get results from start of current year to current time.
})
呀排序是必要的,這需要時間,但記得指數要排序的字段。 –
太棒了!感謝您的提示。我會索引我的排序標準:) – Clement