首页
技术博客
PHP教程
数据库技术
前端开发
HTML5
Nginx
php论坛
新用户注册
|
会员登录
PHP教程
技术博客
编程问答
PNG素材
编程语言
前端技术
Android
PHP教程
HTML5教程
数据库
Linux技术
Nginx技术
PHP安全
WebSerer
职场攻略
JavaScript
开放平台
业界资讯
大话程序猿
登录
极速注册
取消
热门标签 | HotTags
fetch
command
bit
hashset
dagger
match
header
controller
text
stream
usb
frameworks
vba
metadata
timestamp
tags
heatmap
netty
nodejs
hook
integer
eval
httpclient
callback
cookie
js
loops
int
php5
go
perl
foreach
version
golang
solr
main
web
md5
replace
datetime
heap
node.js
spring
expression
select
express
keyword
future
数组
python3
post
scala
jar
python
rsa
split
require
case
cPlusPlus
get
byte
object
uml
hashtable
default
string
bitmap
iostream
function
char
settings
actionscrip
window
install
process
const
blob
join
ip
当前位置:
开发笔记
>
编程语言
> 正文
【问Mongoose】我应该如何处理分页?
作者:佩菱淑华7 | 来源:互联网 | 2023-09-24 16:29
需求场景作者和书籍列表展示列表时如何处理分页数据库如下
需求场景
作者和书籍列表
展示列表时如何处理分页
数据库如下
作者Author
1
2
3
4
5
6
7
8
9
10
// author
let authorSchema = new Schema({
name: {
type: String,
required: true,
index: true,
unique: true
}
})
module.exports = mongoose.model('Author', authorSchema)
书籍Book
1
2
3
4
5
6
7
8
9
10
11
12
// book
let bookSchema = new Schema({
author: {
type: Schema.Types.ObjectId,
ref: 'Author'
},
type: {
type: String,
default: ''
}
})
module.exports = mongoose.model('Book', bookSchema)
查询如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 获取列表-不分组
const getBooks = async (ctx, next) => {
let { size = 10, page = 1 } = ctx.query
let optiOns= {
skip: Number((page - 1) * size),
limit: Number(size)
}
let res = await Book.find({}, null, options)
let total = await Book.countDocuments()
let data = {
list: res,
pagination: {
total,
page,
size
}
}
ctx.body = {
data,
code: 0
}
}
问题:如果我想得到某个作者的书籍列表,有什么好的解决办法么?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 获取列表-如何分组
const getMyBooks = async (ctx, next) => {
let authorId = ctx.authorId
let { size = 10, page = 1 } = ctx.query
let optiOns= {
skip: Number((page - 1) * size),
limit: Number(size)
}
let res = await Book.find({ author: authorId }, null, options)
// ???????????
let total = await Book.countDocuments() // 这个总数应该如何获取比较好?是按authorId查到全部的book,取数组length么?
let data = {
list: res,
pagination: {
total,
page,
size
}
}
ctx.body = {
data,
code: 0
}
}
go
数据库
schema
string
require
export
object
default
const
写下你的评论吧 !
吐个槽吧,看都看了
会员登录
|
用户注册
推荐阅读
go
开发笔记:sql盲注之报错注入(附自动化脚本)
篇首语:本文由编程笔记#小编为大家整理,主要介绍了sql盲注之报错注入(附自动化脚本)相关的知识,希望对你有一定的参考价值。 ...
[详细]
蜡笔小新 2024-09-30 12:32:17
int
SENDMESSAGE函数巧应用
在这一期的SendMessage函数应用中,我将向大家介绍如何利用消息函数来扩展树型列表(TreeView)控件的功能相信对于树型列表控件大家十分的熟悉, ...
[详细]
蜡笔小新 2024-09-29 19:45:57
go
[USACO 2006 November Gold] 玉米地Corn Fields
题目描述 FarmerJohn新买了一块长方形的牧场,这块牧场被划分成M行N列(1<M<12;1<N<12),每一格都是一块正方形的土地。FJ打 ...
[详细]
蜡笔小新 2024-09-29 11:31:29
go
项目需求讨论后台返回的特殊JSON处理
在实际开发中,现在安卓端和后台之间的数据交互,一般都是用JSON来传递数据信息。JSON大家一般都比较熟悉。我这边就以实际项目中的后台传过来的情况和大家分析下及如何处理。比如后台返 ...
[详细]
蜡笔小新 2024-09-30 20:59:48
int
POJ2253(floyd)
FroggerTimeLimit:1000MSMemoryLimit:65536KTotalSubmissions:32257Accepted:10396DescriptionFr ...
[详细]
蜡笔小新 2024-09-30 20:13:09
int
UNP总结 Chapter 12~14 IPv4与IPv6的互操作性、守护进程和inet超级服务器、高级I/O函数
一、IPv4与IPv6的互操作性1.IPv4客户与IPv6服务器拥有双重协议栈的主机的一个基本特性就是:其上运行的IPv6服务器既能应付IPv4客户,又能应付IPv6客户。这是通过使用IPv4映射 ...
[详细]
蜡笔小新 2024-09-30 18:55:51
int
C#开发技巧有哪些
这篇文章将为大家详细讲解有关C#开发技巧有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。C#开发技 ...
[详细]
蜡笔小新 2024-09-30 17:56:32
go
自定义RecyclerView添加EmptyView
你知道RecyclerView里没有Em ...
[详细]
蜡笔小新 2024-09-30 15:31:47
int
C++ pimpl机制详细讲解
PIMPL 是 C++ 中的一个编程技巧,意思为指向实现的指针。具体操作是把类的实现细节放到一个单独的类中,并用一个指针进行访问 ...
[详细]
蜡笔小新 2024-09-30 15:31:40
go
算法递推
DFS基本概念步骤优缺点典型例题递推基本概念直接或者间接调用自身的算法称为递归算法一般数据n ...
[详细]
蜡笔小新 2024-09-30 12:02:17
go
delphi控件大全
本文章已收录于:delphi控件查询:http:www.torry.nethttp:www.jrsoftware.orgTb97最有名的工具条(ToolBar) ...
[详细]
蜡笔小新 2024-09-30 11:49:36
go
2022杭电多校七 FSumire(数位DP+实用技巧)
题目链接:杭电多校7-VirtualJudgevjudge上题目显示的有问题,我下面附上官方题目:样例输入:32201 ...
[详细]
蜡笔小新 2024-09-30 10:31:00
nodejs
ajax 跨域webapi 最简单的demo(只介绍Get)
这几天遇到一个nodejs的项目,使用VSCode开发,需要连接数据库的,但是用nodejs连接数据库比较繁琐,需要安装很多 ...
[详细]
蜡笔小新 2024-09-29 19:42:09
int
C#学习教程:使用RSACryptoServiceProvider进行公钥加密分享
使用RSACryptoServiceProvider进行公钥加密我已经在CodeProject上发表了一篇文章,解释了如何使用RSA提供程序进行加密和解密:RSA私钥加密虽然200 ...
[详细]
蜡笔小新 2024-09-29 18:06:38
go
这个“&”运算符如何指向JavaScript中的数组索引?
我正在学习网络性能课程,其中共享以下代码:Interestingoperatorconstobjects ...
[详细]
蜡笔小新 2024-09-29 14:49:44
佩菱淑华7
这个家伙很懒,什么也没留下!
Tags | 热门标签
fetch
command
bit
hashset
dagger
match
header
controller
text
stream
usb
frameworks
vba
metadata
timestamp
tags
heatmap
netty
nodejs
hook
integer
eval
httpclient
callback
cookie
js
loops
int
php5
go
RankList | 热门文章
1
swift 引入pod_引入自旋:Swift中的通用反馈循环系统
2
hibernate在SSH框架操作ORACLE Clob字段类型的读写改
3
输出呈三角形的杨辉三角形(C语言)
4
WPF设置窗口模式(Windowstyle=“None”)
5
计算机网络设计课程大作业,网络规划与设计课程大作业 某中学校园信息网设计方案...
6
org.greenrobot.greendao.AbstractDao.queryRaw()方法的使用及代码示例
7
Python学习73:hashlib
8
【技术分享】IoTroop:一个正快速扩张的新 IoT僵尸网络分析
9
C#学习教程:我应该如何从控制器动作c#asp.netmvc2返回一个图像?分享
10
【LeetCode】面试题07. 重建二叉树
11
javascript高等程序設計(第三版)進修摘錄下
12
Windows内核开发Windows内部概述2
13
如何在Ubuntu系统下保存屏幕亮度设置?
14
团队项目(任务四):第二次冲刺
15
如何动态修改 new xxx() 的class属性?
PHP1.CN | 中国最专业的PHP中文社区 |
DevBox开发工具箱
|
json解析格式化
|
PHP资讯
|
PHP教程
|
数据库技术
|
服务器技术
|
前端开发技术
|
PHP框架
|
开发工具
|
在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved |
京公网安备 11010802041100号
|
京ICP备19059560号-4
| PHP1.CN 第一PHP社区 版权所有