首先,我想实现一个分页查询统计的功能,需要获取总条数和查询数据。
谁能告诉我为什么我 .count() 后,输出的 list 为啥变成了 [{total: 10}] ,我很不理解?????!!!
const aggregateInstance = db.collection('deal').aggregate().sort({
createTime: -1
}).lookup({
from: 'users',
localField: '_userId',
foreignField: '_id',
as: 'userInfo',
}).lookup({
from: 'stores',
localField: '_storeId',
foreignField: '_id',
as: 'storeInfo',
}).lookup({
from: 'users',
localField: '_operatorId',
foreignField: '_id',
as: 'operatorInfo',
}).match({
'userInfo.nickName': db.RegExp({
regexp: '.*' + keyword,
options: 'i',
})
})
const totalResult = await aggregateInstance.count('total').end()
console.log(totalResult); // {list:[{total: 24}], errMsg: "collection.aggregate:ok"}
const total = totalResult.list[0] && totalResult.list[0].total
const {list} = await aggregateInstance.skip((pageIndex - 1) * pageSize).limit(pageSize).end()
console.log(list); // [{total: 24}]
强制自己记住吧