1,746
社区成员
发帖
与我相关
我的任务
分享
select a.id, a.name, a.xxx
from A a
where exists (select 1 from B b where b.flag=0 and b.pid = a.id)
and a.name like '%xx%'
db.a.insertMany([ {id:1, name:'xx1'}, {id:2, name:'xx2'}, {id:3, name:'aa1'}, {id:4, name:'aa2'} ])
db.b.insertMany([ {id:1, pid:1, flag:0}, {id:2, pid:2, flag:1}, {id:3, pid:3, flag:0}, {id:4, pid:4, flag:1} ])
db.a.aggregate([
{$match:{ name:/xx/ }},
{$lookup:{
from:'b', as:'b_doc',
let:{ id:'$id' },
pipeline:[
{$match:{ flag:0, $expr:{$eq:['$pid', '$$id']} }}
]
}},
{$match:{ 'b_doc._id':{$exists:true} }},
{$project:{ _id:0, b_doc:0 }}
])
db.test1.aggregate([
{$match: {"users.account":"user5"} },
{$project: {id:1, name:1}}
])