有2个集合 tasks 和user
tasks:
{"_id":"d81cd5415f6afe94004ad6361d00aa46","openid":"oN6HT5FH-JO6yIkhlfAZEYRF8d1k","modelType":"1","time":"15:51","date":"2020-09-23","chongfu":"","remark":"213123","status":1,"iswait":1.0}
user:
{"_id":"d81cd5415f6af0000049c9dc0038204a","num":0.0,"openid":"oN6HT5FH-JO6yIkhlfAZEYRF8d1k"}
我想要实现 查询 tasks left join user where( tasks.iswait = 1 and user.num > 0 ) 要实现这种效果。要怎么改呢
db.collection('tasks').aggregate()
.match({
iswait: 1
})
// .lookup({
// from: 'user',
// localField: 'openid',
// foreignField: 'openid',
// as: 'touser',
// })
.lookup({
from: 'user',
let: {
tasks_openid: '$openid',
},
pipeline: $.pipeline()
.match(_.expr($.and([
$.eq(['$openid', '$$tasks_openid']),
$.gt(['$num', 0])
])))
.project({
_id: 0,
openid: 1,
num: 1,
})
.done(),
as: 'touser',
})
.skip(i * MAX_LIMIT).limit(MAX_LIMIT)
.end()
.then((res) => {
})
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/aggregate/Aggregate.lookup.html#%E7%A4%BA%E4%BE%8B