繁体   English   中英

“$lookup with 'pipeline' 不能指定 'localField' 或 'foreignField'”

[英]"$lookup with 'pipeline' may not specify 'localField' or 'foreignField'"

我是 mongoose 和 mongoDB 的新手,我有以下查询,运行时会抛出以下错误。 如果有人可以帮助我处理问题并且仍然得到相同的 output,那就太好了

//interview.model.js => mongodb show name as interview
module.exports = mongoose.model('Interview', interviewSchema);
//candidate.model.js => mongodb show name as candidate
module.exports = mongoose.model('Candidate', candidateSchema);

const [result, err] = await of(Interview.aggregate([
         {
           $match: {
             ...filterQuery,
           }
         },
         {
           $lookup: {
             'from': 'candidates',
             'localField': 'candidateId',
             'foreignField': '_id',
             'as': 'candidateId',
             pipeline: [
               { $match: { 'candidateId.interviewCount': 0 }},
               { $project: { firstName:1, status:1, avatar:1 } }
             ]
           }
         },
       ]))

您可以使用 1) localField/ForeignField 或 2) 使用管道查找的任何一种$lookup语法,

  • let声明一个变量candidateId ,你可以在pipeline中使用这个id,称为localField
  • 使用$expr$eq进行pipeline推送表达式匹配,因为我们正在匹配内部字段
  • $$引用将允许在声明为let的管道中使用变量
const [result, err] = await of(Interview.aggregate([
  { $match: { ...filterQuery } },
  { 
    $lookup: {
      'from': 'candidates',
      'as': 'candidateId',
      'let': { candidateId: "$candidateId" },
      'pipeline': [
        { 
          $match: { 
            $expr: { $eq: ["$$candidateId", "$_id"] },
            'candidateId.interviewCount': 0
          } 
        },
        { $project: { firstName:1, status:1, avatar:1 } }
      ]
    }
  }
]))

0

使用 $lookup 中的管道,您不能使用 localField 或 foreignField。 有关语法和示例,请参阅MongoDB文档...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM