Error implementing Querying after populate in Mongoose [duplicate]

[亡魂溺海] 提交于 2019-12-11 18:48:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!