【问题标题】:how to use find in populate (mongoose)如何在填充(猫鼬)中使用查找
【发布时间】:2021-04-19 10:41:00
【问题描述】:

我想用员工 ID、员工姓名、项目名称匹配一个字符串

我的模特:(员工)

const employeeSchema = new mongoose.Schema(
  {
    employeeID: { type: String, unique: true, required: true },
    departmentID: { type: mongoose.Types.ObjectId, ref: 'Department', default: null },
    employeeName: { type: String, required: true },
    gender: { type: String, default: "", enum: ["Male", "Female"] },
    designation: { type: String, default: "" },
    email: { type: String, default: null },
    branchName: { type: String, default: "" },
    employeeType: { type: String, default: "" },
    jobStatus: { type: String, default: "" },
    joiningDate: { type: Date, default: "" },
    leavingDate: { type: Date, default: "" },
    comments: { type: String, default: "" },
    managerID: { type: mongoose.Types.ObjectId, ref: 'Employee', default: null },
    region: { type: String, default: "" },
  }
);

(项目)

const projectSchema = new mongoose.Schema(
  {
    projectName: {type: String, required: true},
    tech: {type: String, required: true},
    startDate: {type: Date, required:true},
    endDate: {type: Date, required:true},
    customerID:{type:mongoose.Types.ObjectId, ref:'Customer'}
  }
);

(员工-项目)

const employeeProjectSchema = new mongoose.Schema(
  {
    empID: {type: mongoose.Types.ObjectId, required: true, ref: 'Employee'},
    projectID: {type: mongoose.Types.ObjectId, required: true, ref: 'Project'},
    allocation: {type: [Number]},
    assignStartDate: {type: Date},
    assignEndDate: {type: Date},
  }
);

我正在从作为我的主表的员工项目表中运行查询。

我想做的是将字符串与employeeID 和employeeName(employee table) 以及projectName(project table) 匹配。 我现在在做什么(这是错误的)

async rpSearch(data): Promise<any> {
    try {
      const result = await MongooseEmployeeProject.find()
      .populate({
        path: 'empID',
        select: 'employeeID employeeName designation region comments resourceManager',
        match:{
          type:{data}
        }
      })
      .populate({
        path: 'projectID',
        select: 'projectName tech startDate endDate customerID'
        match:{
          type:{data}
      })

请指导我

【问题讨论】:

    标签: mongodb mongoose mongoose-populate


    【解决方案1】:

    首先,path 必须是架构的字段名称。您将path 设置为“employeeID”,但“empID”作为employeeProjectSchema 的字段名称是正确的。

    其次,您设置了match,但您的employeeSchemaemployeeProjectSchema 中都没有type 字段。所以你必须删除match

    async rpSearch(data): Promise<any> {
        try {
          const result = await MongooseEmployeeProject.find()
          .populate({
            path: 'empID',
            select: 'employeeID employeeName designation region comments resourceManager'
          })
          .populate({
            path: 'projectID',
            select: 'projectName tech startDate endDate customerID'
          })
    

    【讨论】:

    • 搞错了,和你说的一样
    猜你喜欢
    • 2015-09-30
    • 1970-01-01
    • 2017-10-11
    • 2019-12-20
    • 2015-09-08
    • 2018-02-19
    • 2021-09-27
    • 2016-06-11
    • 2019-07-17
    相关资源
    最近更新 更多