match 聚合查询,我想过滤数组,nin和in为什么不生效? 关联查询后如何过滤数组?
const users = await userDb.where({
openId: OPENID,
}).get();
const currentUser = users.data[0] || {}
const where = {
active: true,
openId: $.neq(OPENID), // 不能看到自己的小程序
}
if (taskType === 'taskRecord') {
if (users.data && users.data.length) { // 只看自己点过的小程序
where.accessApp = _.in(currentUser.accessApp)
console.log('查询点过的', currentUser)
}
} else {
// 查看最新笔记
where.flowers = $.gt(0); // 小红花必须大于0
if (users.data && users.data.length) { // 过滤已看过的小程序
where.accessApp = _.nin(currentUser.accessApp)
console.log('查询最新笔记', currentUser)
}
}
const lookup = {
from: 'user',
localField: 'openId',
foreignField: 'openId',
as: 'userInfo',
}
const addFields = {
flowers: '$userInfo.flowers',
accessApp: "$userInfo.accessApp"
}
const countAggregateInstance = task.aggregate().lookup(lookup);
// 统计数量
const totalCount = await countAggregateInstance.addFields(addFields).match(where).count('totalCount').end();
// 分页查询
const data = await task
.aggregate()
.lookup(lookup)
.addFields(addFields)
.match(where)
.skip((pageIndex - 1) * pageSize)
.limit(pageSize)
.end()