问题
Hello recently i asked a question: How can i find documents by populated field? What is a which turned out to be a duplicate: Querying after populate in Mongoose
Im got already
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());
};
from log pipeline i got
[
{
"$lookup":{
"from":"companies",
"as":"company",
"let":{
"company":"$company"
},
"pipeline":[
{
"$match":{
"company.name":{
"$eq":"Test"
},
"$expr":{
"$in":[
"$_id",
"$$company"
]
}
}
}
]
}
}
]
But its returning for me all of Policies (companies field is empty) but only one of them has company named Test :(
A Screenshoot for easier understanding: https://paste.pics/73efbe10afefc1963dcf75e312e95663
来源:https://stackoverflow.com/questions/58124437/error-implementing-querying-after-populate-in-mongoose