小编Nal*_*uza的帖子

8
推荐指数
4
解决办法
2727
查看次数

typeorm mongo db 中的一对一关系

我如何使用 Typeform MongoDB 实现一对一的关系。

我正在尝试使用聚合加入两个文档,但没有成功。

请提供一种解决方案。

实体详细信息 1.country :- _id,name 2.state :- _id,name, countryid

export class Country {
    @ObjectIdColumn()
    @Type(() => String)
    _id: ObjectID;


    @Column()
    @IsString()
    @IsNotEmpty()
    @Index({unique: true})
    name: string;

}
Run Code Online (Sandbox Code Playgroud)
@Entity()
export class State {
    @ObjectIdColumn()
    @Type(() => String)
    _id: ObjectID;

    @Column()
    @IsNotEmpty()
    name: string;

    @ObjectIdColumn({nullable: false})
    @IsNotEmpty()
    countryId: ObjectID;

}
Run Code Online (Sandbox Code Playgroud)

查找代码(选择所有状态)

 let stateRepository: StateRepository = getCustomRepository(StateRepository);
        try {
const states = await stateRepository.aggregate([
                {
                    $lookup:
                    {
                        from: 'country',
                        localField: 'countryId',
                        foreignField: '_id',
                        as: 'country'
                    }
                }
            ]); …
Run Code Online (Sandbox Code Playgroud)

find aggregation mongodb typeorm

8
推荐指数
1
解决办法
1443
查看次数