我试图在MongoDB中执行以下操作(来自SQL),但似乎无法掌握它。我认为诀窍是放松,但我不确定。
select p.id, count(*)
from model.patients p
join model.patientAddresses pa on pa.PatientId = p.Id
where p.lastname like '%a%' and not exists
(select *
from model.patients p2
join model.PatientAddresses pa2 on pa2.PatientId = p2.Id
where pa2.relatedpatientid = p.id
)
group by p.id
having count(*) >1
我有:
db.Foo.aggregate( [ { $unwind : "$relatedNames" },
{ $match: { relatedNames: 'x,' name: 'x'}] )
" Foo"的相应结构。在MongoDB中是:
{
name: ''
relatedNames: ['', '', '', '']
}
我想找到名字=' x'并且没有其他Foo,其中RelatedNames包含' x'
协助表示赞赏。