在 MongoDb 中使用 join 我可以获得我想要的值
(await this.ideasCollection).aggregate([
{
$match: { userId, deleted: { $ne: true } },
},
{
$lookup: {
from: 'teams',
localField: '_id',
foreignField: `ideas.daily.ideaId`,
as: 'teams',
},
},
])
但是当我尝试使用管道以获得更少的字段(使用 $project)时,它不起作用
(await this.ideasCollection).aggregate([
{
$match: { userId, deleted: { $ne: true } },
},
{
$lookup: {
from: 'teams',
let: { ideaId: '$_id' },
pipeline: [
{ $match: { $expr: { $eq: ['$ideas.daily.ideaId', '$$ideaId'] } } },
{ $project: { _id: 1, name: 1 } },
],
as: 'teams',
},
},
])
是否有某种技巧可以使第二个选项起作用?
这是团队的结构
{
"_id": { "$oid": "58948652ab5f580010faeb44" },
"name": "My team",
"ideas": {
"daily": [
{ "ideaId": { "$oid": "5b6d913e3e552800260904e1" }, "ordinal": 0 },
{ "ideaId": { "$oid": "5bbbc93459914f0013e3c522" }, "ordinal": 1 },
]
}
}
谢谢