如何在猫鼬中加入并运行NOT EXISTS查询

时间:2019-07-10 07:11:08

标签: javascript sql node.js mongodb mongoose

我有一个SQL查询,需要将其转换为猫鼬聚合

SQL:

Select a.participant_id, a.name 
from participant a , settlementAccount b 
Where a.settlementAccount = b.id
and b.account_type = "FundingAgent"
and Not Exists   
(select 1 from settlementAccount c where  b.account_type = "Tracking" and a.id = c.owner_id)

我试图聚合成猫鼬,但卡在查询的Not Exists部分。

猫鼬:

Participant.aggregate([{
  $lookup: {
    from: 'settlementAccount', // collection name in db
    localField: 'settlementAccount',
    foreignField: 'id',
    as: 'settlementOwners'
  }
},
  {
    $match:
      {
        $and:
          [
            {'settlementOwners.account_type': 'FundingAgent'},
            {'settlementOwners.account_type': {$ne: 'Tracking'}},
            {'id': {$ne: 'settlementOwners.owner_id'}}
          ]

      }
  },
  {$project: {_id: '$_id', id: '$id', name: '$name'}}
])

我不知道这是否是正确的实现。 有谁知道如何将这些查询组合在一起?

0 个答案:

没有答案