将MongoDB聚合中的匹配和查找与布尔或

将MongoDB聚合中的匹配和查找与布尔或,mongodb,Mongodb,我想在MongoDB中完成以下类似SQL的操作 SELECT contentItem._id, contentitem.text FROM contentItems WHERE contentItem.metadata.color = 'yellow' UNION SELECT contentItem._id, contentitem.text FROM contentItems WHERE contentItems.contentSets IN ( SELECT contentSet.

我想在MongoDB中完成以下类似SQL的操作

SELECT contentItem._id, contentitem.text
FROM contentItems
WHERE contentItem.metadata.color = 'yellow'
UNION
SELECT contentItem._id, contentitem.text
FROM contentItems
WHERE contentItems.contentSets IN (
    SELECT contentSet._id FROM contentSets WHERE contentSet.metadata.color= 'yellow'
)
也可以这样做

SELECT contentItem._id, contentitem.text
FROM contentItems
WHERE (
    contentItem.metadata.color = 'yellow'
) OR contentItems.contentSets IN (
    SELECT contentSet._id FROM contentSets WHERE contentSet.metadata.color = 'yellow'
)
这两部分我都能做

db.getCollection('contentItems').aggregate([
    {
        '$match': {
            'metadata.color': { '$eq': 'yellow' }
        }
    }
])


但我不知道如何获得这些数据的并集。

我想出了在匹配中使用
$或
运算符的办法

db.getCollection('contentItems').aggregate([
    {
        '$lookup': {
            'from': 'contentSets',
            'localField': 'contentSets',
            'foreignField': '_id',
            'as': 'matchedContentSets'
        }
    },
    {
        '$match': {
            '$or': [
                { 'metadata.color': { '$eq': 'yellow' } },
                { 'matchedContentSets.metadata.color': { '$eq': 'yellow' } },
            ]
        }
    },
    {
        '$project': {
            'matchedContentSets': 0
        }
    }
])

能否粘贴sampe文档和预期输出?以及样本输出
db.getCollection('contentItems').aggregate([
    {
        '$lookup': {
            'from': 'contentSets',
            'localField': 'contentSets',
            'foreignField': '_id',
            'as': 'matchedContentSets'
        }
    },
    {
        '$match': {
            '$or': [
                { 'metadata.color': { '$eq': 'yellow' } },
                { 'matchedContentSets.metadata.color': { '$eq': 'yellow' } },
            ]
        }
    },
    {
        '$project': {
            'matchedContentSets': 0
        }
    }
])