我有两个集合"new_user"和"workspace".我正在try 执行查找查询,但它在预期的字段中只给出了空array.
我的‘new_Users’集合:
export class UserEntity {
@Prop({ required: false, type: Types.ObjectId, default: () => new ObjectId() })
_id?: Types.ObjectId;
@Expose()
@Prop({ default: '', trim: true, text: true })
firstName: string;
@Expose()
@Prop({ default: '', trim: true, text: true })
lastName: string;
}
我的"工作空间"Collection :
export class WorkspaceUsers {
@Expose()
@Prop({ type: () => [WorkspaceUserGroup] })
groups: WorkspaceUserGroup[];
@Expose()
@Prop({ required: true, type: String, enum: WorkspaceRoleEnum })
workSpaceRole: WorkspaceRoleEnum;
@Expose()
@Prop({ required: true, type: String, ref: () => UserEntity })
userID: string;
}
export class WorkspaceEntity {
@Prop({ required: false, type: Types.ObjectId, default: () => new ObjectId() })
_id?: Types.ObjectId;
@Expose()
@IsString()
@IsDefined()
@Prop({ required: true, trim: true })
name: string;
@Expose()
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => WorkspaceUsers)
@Prop({ required: false, type: () => [WorkspaceUsers], default: [] })
users: WorkspaceUsers[];
}
现在,我想在我的工作区模型中运行查找查询,如下所示:
const aggregationQuery: PipelineStage[] = [];
aggregationQuery.push({
$unwind: '$users',
});
aggregationQuery.push({
$lookup: {
from: 'new_users',
localField: 'users.userID',
foreignField: '_id',
as: 'userData',
},
});
它在用户数据字段中返回一个空array.我到底做错了什么?