您好,最近我问了一个问题: How can i find documents by populated field? 什么是重复的结果: Querying after populate in Mongoose
我已经有
PolicySchema.statics.lookup = async function({path, query}) {
const rel =
mongoose.model(this.schema.path(path).options.ref);
const pipeline = [
{ '$lookup': {
'from': rel.collection.name,
'as': path,
'let': { [path]: `$${path}` },
'pipeline': [
{ '$match': {
...query,
'$expr': { '$in': [ '$_id', `$$${path}` ] },
}},
],
}},
];
console.log(JSON.stringify(pipeline));
console.log(await this.aggregate(pipeline).exec());
};
我从日志管道中获得
[
{
"$lookup":{
"from":"companies",
"as":"company",
"let":{
"company":"$company"
},
"pipeline":[
{
"$match":{
"company.name":{
"$eq":"Test"
},
"$expr":{
"$in":[
"$_id",
"$$company"
]
}
}
}
]
}
}
]
但是它为我返回了所有策略(“公司”字段为空),但只有其中一个拥有名为Test的公司:(
便于理解的屏幕截图:https://paste.pics/73efbe10afefc1963dcf75e312e95663