【发布时间】:2017-05-23 13:52:19
【问题描述】:
我正在努力研究如何有效地对数据进行分页。如果我有一个 URL,例如:example.com/items?page=5,我怎样才能每次都检索到正确的记录(这样同一记录就不会出现在另一个页面上)?我想我必须先对数据库记录进行排序,然后使用 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.
})
【问题讨论】:
-
是的,排序是必要的,这需要时间,但请记住为要排序的字段编制索引。
-
太棒了!谢谢你的提示。我将索引我的排序标准:)
标签: javascript node.js mongodb pagination