【发布时间】:2019-05-31 11:56:52
【问题描述】:
我有以下 mongo 聚合查询:
return db.collection('projects').aggregate([
{
$match: {
agents: ObjectId(agent)
}
},
{
$lookup: {
from: "agents",
localField: "agents",
foreignField: "_id",
as: "agents"
}
},
{
$lookup: {
from: "roles",
localField: "roles",
foreignField: "_id",
as: "roles"
}
},
{
$lookup: {
from: "agencies",
localField: "agency",
foreignField: "_id",
as: "agency"
}
},
{
$lookup: {
from: "resources",
localField: "roles.applicants",
foreignField: "_id",
as: "roles.applicants"
}
}
])
它可以正常工作,嵌入正确的文档。但是,每个申请人都会显示“password_hash”字段。我想删除该字段。如果我尝试投影和设置roles.applicants.password_hash: 0,我实际上最终会得到整个申请人而没有密码哈希,但其余的roles 字段不再存在。所以我得到的东西看起来像:
roles: {
applicants: {
name: "Josh"
}
}
应该是这样的
roles: {
title: "Super Hero",
applicants: {
name: "Josh"
}
}
【问题讨论】:
标签: mongodb aggregation projection