Mongo查找条件:不存在

huangapple go评论58阅读模式
英文:

Mongo lookup condition: not exist

问题

We have two collections.

One is the books collection with loads of data. Like:

{
   '_id': {"$oid": "64ba6a416504f1f01d773faa"},
   'isbn': 8765434567890,
   'booktitle': 'some text',
}
{
   '_id': {"$oid": "64ba6a416504f1f01d773faa"},
   'isbn': 8765434566666,
   'booktitle': 'other book',
}

The notes collection has documents with extra info about some of the documents in the base collection. Like:

{
   '_id': {"$oid": "64ba6a416504f1f01d773faa"},
   'isbn': 8765434567890,
   'note': 'blabla',
}

So not all documents in books have a counterpart in notes. If there is, it's one-to-one.

What I try to find out is an aggregation that shows all of the books documents (with possibly additional {match} stuff), that specifically do not have a counterpart in the notes collection.

[
   {'$match': {}},
   {'$lookup':
      {
        'from': 'notes',
		'localField': 'isbn',
		'foreignField': 'isbn',
		'as': 'related_note',
      }
   },
   {'$project':
      {
         'isbn': '$isbn',
         'title': '$title',
         'note': '-- no notes about this title',
      }
   }
]

So not a left-join, not a right-join but a: NOT(left-join)

Anybody? Thanks.

英文:

We have two collections.

One is the books collection with loads of data. Like:

{
   '_id' {"$oid": "64ba6a416504f1f01d773faa"},
   'isbn': 8765434567890,
   'booktitle': 'some text',
}
{
   '_id' {"$oid": "64ba6a416504f1f01d773faa"},
   'isbn': 8765434566666,
   'booktitle': 'other book',
}

The notes collection has documents with extra info about some of the documents in the base collection. Like:

{
   '_id' {"$oid": "64ba6a416504f1f01d773faa"},
   'isbn': 8765434567890,
   'note': 'blabla',
}

So not all documents in books have a counterpart in notes. If there is, it's one-to-one.

What I try to find out is an aggregation that shows all of the books documents (with possibly additinal {match} stuff), that specifically do not have a counterpart in the notes collection.

[
   {'$match': {}},
   {'$lookup':
      {
        'from': 'notes',
		'localField': 'isbn',
		'foreignField': 'isbn',
		'as': 'related_note',
      }
   },
   {'$project':
      {
         'isbn': '$isbn',
         'title': '$title',
         'note': '-- no notes about this title',
      }
   }
]

So not a left-join, not a right-join but a: NOT(left-join)

Anybody? Thanks.

答案1

得分: 3

以下是翻译好的部分:

"What I try to find out is an aggregation that shows all of the books documents (with possibly additinal {match} stuff), that specifically do not have a counterpart in the notes collection."
我试图查找的是一个聚合操作,显示所有的书籍文档(可能包括其他{match}的内容),这些文档在笔记集合中没有对应项。

"Would say that your result should be LEFT JOIN but excluding intersection."
可以说你的结果应该是LEFT JOIN,但要排除交集。

"By default, MongoDB $lookup performs as the LEFT JOIN. To exclude the documents which are under the intersection, you can filter with related_note: [] via the $match stage."
默认情况下,MongoDB的$lookup执行左连接操作。要排除在交集下的文档,可以通过$match阶段使用related_note: []进行过滤。

Demo @ Mongo Playground

英文:

> What I try to find out is an aggregation that shows all of the books documents (with possibly additinal {match} stuff), that specifically do not have a counterpart in the notes collection.*

Would say that your result should be LEFT JOIN but excluding intersection.

Mongo查找条件:不存在

Reference: SQL joins as Venn diagram

By default, MongoDB $lookup performs as the LEFT JOIN. To exclude the documents which are under the intersection, you can filter with related_note: [] via the $match stage.

db.books.aggregate([
  //{
  //  "$match": {}
  //},
  {
    "$lookup": {
      "from": "notes",
      "localField": "isbn",
      "foreignField": "isbn",
      "as": "related_note"
    }
  },
  {
    $match: {
      related_note: []
    }
  },
  {
    "$project": {
      "isbn": "$isbn",
      "title": "$title",
      "note": "-- no notes about this title",
      
    }
  }
])

Demo @ Mongo Playground

huangapple
  • 本文由 发表于 2023年7月24日 19:46:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定