MongoDB - 相当于不存在一个集合的 LEFT JOIN

Pal*_*mer 5 javascript mongodb mongodb-query aggregation-framework

是否有等效于 LEFT JOIN 查询的 MongoDB 中不存在正确集合的查询?

查询语句:

SELECT * FROM TableA as A LEFT JOIN TableB as B ON A.id = B.id 
WHERE B.Id IS NULL
Run Code Online (Sandbox Code Playgroud)

MongoDB: ???

PS:我最初的草图:

db.getCollection('collA').aggregate([
    {
      $lookup:
        {
          from: "collB",
          localField: "_id",
          foreignField: "_id",
          as: "collB"
        }           
   }
   //, {$match : collB is empty}
])
Run Code Online (Sandbox Code Playgroud)

Nei*_*unn 9

那么你的编辑基本上有答案。只是$match数组为空的地方:

db.getCollection('collA').aggregate([
    { "$lookup": {
      "from": "collB",
      "localField": "_id",
      "foreignField": "_id",
      "as": "collB"
    }},
   { "$match": { "collB.0": { "$exists": false } } }
])
Run Code Online (Sandbox Code Playgroud)

$exists对数组索引的测试0是在查询中询问“这是一个包含项目的数组”的最有效方法。