我想在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([
{'$unwind': '$contentSets'},
{
'$lookup': {
'from': 'contentSets',
'localField': 'contentSets',
'foreignField': '_id',
'as': 'matchedContentSets'
}
},
{
'$match': {
'matchedContentSets.metadata.color': { '$eq': 'yellow' }
}
}
])
但我不知道如何把这些数据结合起来。