我有这个查询并且有效
Job.aggregate([
{$match: { created_by: userId }},
{$lookup: {
from: "users",
localField: "applicant._id",
foreignField: "_id",
as: "applicant" }},
{$unwind: {
path :'$applicant',
preserveNullAndEmptyArrays: true
}
}
])
.then(data=>{
res.json({status:1, data})
})
问题是申请人带来的密码是坏的。我可以在回调中进行操作,但无论如何我可以使用mongodb的查询过滤出来吗?
我知道我可以像find那样在findOne中进行过滤
User.findOne({id: user_id, {password: 0, _v: 0}).exec()
答案 0 :(得分:0)
使用$project
禁用结果
Job.aggregate([
{$match: { created_by: userId }},
{$lookup: {
from: "users",
localField: "applicant._id",
foreignField: "_id",
as: "applicant" }},
{$unwind: {
path :'$applicant',
preserveNullAndEmptyArrays: true
}
},
{$project : {"applicant.password" : 0}} // disable -> 0
])
.then(data=>{
res.json({status:1, data})
})