查找后从嵌入式投影文档中删除字段

时间:2019-01-04 22:20:40

标签: mongodb aggregation projection

我有以下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"
 }
}

1 个答案:

答案 0 :(得分:0)

我知道了。这是我的方法。首先,为什么角色文档缺少字段并不是问题所在。这是查找。

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

File logbackFile = new File(Environment.getExternalStorageDirectory(), "logback/gatewaylog.txt");
            if (logbackFile.length() == 0) {
                view.hideLogsExportProgress();
                return;
            }

            RequestBody logBackRequestFile = RequestBody.create(MediaType.parse("text/plain"), logbackFile);
            MultipartBody.Part logBackMultipart = MultipartBody.Part.createFormData("file", logbackFile.getName(), logBackRequestFile);

            compositeDisposable.add(apiRepository.uploadLogFile(logBackMultipart, Constants.GATEWAY).observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(status -> {
                        view.hideLogsExportProgress();
                        if (status != null) {
                            view.showError("Successfully logs uploaded to cloud");
                        } else {
                            view.showError("Failed to uploaded logs to cloud. Try again.");
                        }
                    }, throwable -> {
                        view.hideLogsExportProgress();
                        view.showError("Server not reachable");
                        throwable.printStackTrace();
                    }));

整个集合看起来像这样

 {
    $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"
    }
  }