如何使用嵌套查找查询?

问题描述 投票:0回答:0

我有两个集合“new_users”和“workspaces”。我正在尝试进行查找查询,但它只在预期字段中提供空数组。

我的“新用户”收藏:

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;
}

我的“工作区”收藏:


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',
        },
    });

它在 userData 字段中返回一个空数组。我在这里做错了什么?

typescript database mongodb nosql typegoose
© www.soinside.com 2019 - 2024. All rights reserved.