【发布时间】:2019-06-10 19:45:25
【问题描述】:
我正在寻找按位于 collection2 上的标签对 collection1 进行分组 这两个集合需要通过 2 个字段 (field1, field2) 连接(查找)
到目前为止,我想出了以下查询:
db.collection1.aggregate([
{
"$lookup": {
"from": "collection2",
"let": { _field1: '$field1', _field2: '$field2' },
"pipeline": [{
"$match": {
"$expr": {
"$and": [
{ "$eq": ["$field1", "$$_field1"] },
{ "$eq": ["$field2", "$$_field2"] }
]
}
}
},
{ "$project": { _id: 0, tags: 1 } },
],
"as": "col2"
}
},
{ "$unwind": "$col2" },
{ $group: { _id: "$col2.tags", count: { $sum: 1 } } }
]);
我没有得到任何结果。
field1 和 field2 在 collection2 中是唯一的(具有唯一索引)
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework