在分页查询中,一般是需要返回符合条件的文档总数的,下面的代码是先获取文档总数count,然后获取0-20条数据。这两部分的代码高度重复,如果查询条件复杂的话,更是十分冗长。
// 文档总数量count读取
let queryCount = db.collection('cats-order').aggregate()
.match(orderMatch)
.project({
shopId: true,
customerId: true,
status: true,
services: true,
createTime: true,
})
.lookup({
from: 'cats-shop',
let : {
shopId: '$shopId'
},
pipeline: $.pipeline()
.match(_.expr($.and([
$.eq(['$_id', '$$shopId'])
])))
.project({
_id: false,
shopTitle: '$title'
})
.done(),
as: 'shops'
})
.replaceRoot({
newRoot: $.mergeObjects([$.arrayElemAt(['$shops', 0]), '$$ROOT'])
})
.project({
shops: false
})
.lookup({
from: 'cats-customer',
let: {
customerId: '$customerId'
},
pipeline: $.pipeline()
.match(_.expr($.and([
$.eq(['$_id', '$$customerId'])
]))).project({
_id: false,
customerTitle: '$title',
customerPhone: '$phone',
customerLevel: '$level'
}).done(),
as: 'customers'
})
.replaceRoot({
newRoot: $.mergeObjects([$.arrayElemAt(['$customers', 0]), '$$ROOT'])
})
.project({
customers: false
})
if (searchWord) {
queryCount = queryCount.match(_.or([{
customerTitle: new RegExp(searchWord, 'i')
}, {
customerPhone: new RegExp(searchWord, 'i')
}]))
}
let countRes = await queryCount.count('total').end()
pager.Total = countRes.list[0].total
// 分页数据读取
let queryList = db.collection('cats-order').aggregate()
.match(orderMatch)
.project({
shopId: true,
customerId: true,
status: true,
services: true,
createTime: true,
})
.lookup({
from: 'cats-department',
let : {
shopId: '$shopId'
},
pipeline: $.pipeline()
.match(_.expr($.and([
$.eq(['$_id', '$$shopId'])
])))
.project({
_id: false,
shopTitle: '$title'
})
.done(),
as: 'shops'
})
.replaceRoot({
newRoot: $.mergeObjects([$.arrayElemAt(['$shops', 0]), '$$ROOT'])
})
.project({
shops: false
})
.lookup({
from: 'cats-customer',
let: {
customerId: '$customerId'
},
pipeline: $.pipeline()
.match(_.expr($.and([
$.eq(['$_id', '$$customerId'])
]))).project({
_id: false,
customerTitle: '$title',
customerPhone: '$phone',
customerLevel: '$level'
}).done(),
as: 'customers'
})
.replaceRoot({
newRoot: $.mergeObjects([$.arrayElemAt(['$customers', 0]), '$$ROOT'])
})
.project({
customers: false
})
if (searchWord) {
queryList = queryList.match(_.or([{
customerTitle: new RegExp(searchWord, 'i')
}, {
customerPhone: new RegExp(searchWord, 'i')
}]))
}
let listRes = await queryList.sort({createTime: -1}).skip(pager.Offset).limit(pager.Limit).end()
return {
data: listRes.list,
pager, // 返回{ data, pager: {Offset, Limit, Total }}这样的数据结构,是为了和云数据库的http读取方式统一
errMsg: 'collection.aggregate:ok'
}
https://developers.weixin.qq.com/community/develop/article/doc/0008ea04120a18f6988b86d065b013
参考一下。
在云函数里分页,比较麻烦,数据不大的话,不如一次全取。
上面代码中,queryCount和queryList是完全一样的,只不过 一个是queryCount.count('total').end(), 一个是queryList.skip(pager.Offset).limit(pager.Limit).end(), 我尝试过用queryList = queryCount来简化代码,可是发现queryCount.count('total').end()之后,queryList就不能按预期那样读取分页数据了。 请问像这种分页读取需要同时返回文档总数的,一般是如何写比较简洁的代码的? 谢谢!