【问题标题】:Remove field from embedded projection document after lookup查找后从嵌入的投影文档中删除字段
【发布时间】:2019-05-31 11:56:52
【问题描述】:

我有以下 mongo 聚合查询:

    return db.collection('projects').aggregate([
  {
    $match: {
      agents: ObjectId(agent)
    }
  },
  {
    $lookup: {
      from: "agents",
      localField: "agents",
      foreignField: "_id",
      as: "agents"
    }
  },
  {
    $lookup: {
      from: "roles",
      localField: "roles",
      foreignField: "_id",
      as: "roles"
    }
  },
  {
    $lookup: {
      from: "agencies",
      localField: "agency",
      foreignField: "_id",
      as: "agency"
    }
  },
  {
    $lookup: {
      from: "resources",
      localField: "roles.applicants",
      foreignField: "_id",
      as: "roles.applicants"
    }
  }
])

它可以正常工作,嵌入正确的文档。但是,每个申请人都会显示“password_hash”字段。我想删除该字段。如果我尝试投影和设置roles.applicants.password_hash: 0,我实际上最终会得到整个申请人而没有密码哈希,但其余的roles 字段不再存在。所以我得到的东西看起来像:

roles: {
 applicants: {
  name: "Josh"
 }
}

应该是这样的

roles: {
 title: "Super Hero",
 applicants: {
  name: "Josh"
 }
}

【问题讨论】:

    标签: mongodb aggregation projection


    【解决方案1】:

    我想通了。这是我的做法。首先,投影不是角色文档缺少字段的问题。这是查找。

    我所做的是将角色查找更改为在嵌套文档上使用管道方法,然后对申请人进行另一个管道,首先匹配资源集合中的申请人,然后处理投影。看起来像这样。

     {
        $lookup: {
          from: "roles",
          let: { "roles": "$roles" },
          pipeline: [
            { $match: { $expr: { $in: [ "$_id", "$$roles" ] } } },
            {
              $lookup: {
                from: "resources",
                let: { "applicants": "$applicants" },
                pipeline: [
                  { $match: { $expr: { $in: [ "$_id", "$$applicants" ] } } },
                  {
                    $project: {
                      first_name: 1
                    }
                  }
                ],
                as: "applicants"
              }
            }
          ],
          as: "roles"
        }
      }
    

    整个聚合看起来像这样

    return db.collection('projects').aggregate([
      {
        $match: {
          agents: ObjectId(agent)
        }
      },
      {
        $lookup: {
          from: "agents",
          localField: "agents",
          foreignField: "_id",
          as: "agents"
        }
      },
      {
        $lookup: {
          from: "agencies",
          localField: "agency",
          foreignField: "_id",
          as: "agency"
        }
      },      
      {
        $lookup: {
          from: "roles",
          let: { "roles": "$roles" },
          pipeline: [
            { $match: { $expr: { $in: [ "$_id", "$$roles" ] } } },
            {
              $lookup: {
                from: "resources",
                let: { "applicants": "$applicants" },
                pipeline: [
                  { $match: { $expr: { $in: [ "$_id", "$$applicants" ] } } },
                  {
                    $project: {
                      first_name: 1
                    }
                  }
                ],
                as: "applicants"
              }
            }
          ],
          as: "roles"
        }
      }
    ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 2012-08-12
      • 2014-08-19
      相关资源
      最近更新 更多