问题描述
我想将一个字符串与员工 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,gender: { type: String,default: "",enum: ["Male","Female"] },designation: { type: String,default: "" },email: { type: String,branchName: { type: String,employeeType: { type: String,jobStatus: { type: String,joiningDate: { type: Date,leavingDate: { type: Date,comments: { type: String,managerID: { type: mongoose.Types.ObjectId,ref: 'Employee',region: { type: String,}
);
(项目)
const projectSchema = new mongoose.Schema(
{
projectName: {type: String,required: true},tech: {type: String,startDate: {type: Date,required:true},endDate: {type: Date,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,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}
})
请指导我
解决方法
首先,path
必须是架构的字段名称。您将 path
设置为“employeeID”,但“empID”作为employeeProjectSchema 的字段名称是正确的。
其次,您设置了 match
,但您的 type
和 employeeSchema
中都没有 employeeProjectSchema
字段。所以你必须删除 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'
})