如何使用mongodb在typegraphql中存储引用的子文档?
我有一个收藏店,另一个是会员。我想在收集商店里储存数据 结构如下:如何使用mongodb在typegraphql中存储引用的子文档?,mongodb,typescript,typeorm,typegraphql,Mongodb,Typescript,Typeorm,Typegraphql,我有一个收藏店,另一个是会员。我想在收集商店里储存数据 结构如下: @ObjectType('ShopType') @InputType('ShopInput') @Entity() class Shop{ @Field(() => String, { nullable: false }) @prop({ unique: true, required: true }) public id!: string; @Field(() => String
@ObjectType('ShopType')
@InputType('ShopInput')
@Entity()
class Shop{
@Field(() => String, { nullable: false })
@prop({ unique: true, required: true })
public id!: string;
@Field(() => String, { nullable: false })
@prop({ unique: false, required: true })
public name!: string;
@Field(() => String, { nullable: false })
@prop({ unique: true, required: true })
public logo_url!: string;
@Field(() => String, { nullable: false })
@prop({ unique: true, required: true })
public registration_no!: string;
@Field(type => [Memeber], { nullable: true })
@prop({
unique: false, required: false, ref: () => [Memeber],
type: () => [Schema.Types.Mixed]. // Taken from package mongoose
})
public members?: Ref<Memeber>[];
}
@ObjectType('ShopType'))
@InputType('ShopInput')
@实体()
班级商店{
@字段(()=>字符串,{nullable:false})
@prop({unique:true,required:true})
公共id!:字符串;
@字段(()=>字符串,{nullable:false})
@prop({unique:false,required:true})
公共名称!:字符串;
@字段(()=>字符串,{nullable:false})
@prop({unique:true,required:true})
公共徽标_url!:字符串;
@字段(()=>字符串,{nullable:false})
@prop({unique:true,required:true})
公共注册号!:字符串;
@字段(类型=>[Memeber],{nullable:true})
@支柱({
唯一:false,必需:false,ref:()=>[Memeber],
类型:()=>[Schema.Types.Mixed].//取自包mongoose
})
公共成员?:参考文献[];
}
与成员集合相同
但我遇到了错误:
商店验证失败:memebers.0:对值“Document{\n id:>'emp1.1',\n category:”的ObjectId转换失败
“,\n名称:'3',\n加入日期:160027449}”在路径“memebers”,>成员:'3',\n加入日期:160027449}”在路径“memebers”,成员:
对值“[成员{\n id:'emp1.1',\n的数组强制转换失败 类别:'服务员',\n所有者\u id:'3',\n加入日期:1600279449} ]“位于路径”成员“”
很抱歉,这个问题已经由我解决了,因为我主要关注于使用typeorm来完成这项工作,但是创建模型的是typegoose。 以下代码供解决方案参考:- 模型公司(上级单据) 上面的“ref”是并且从不做ref:()=>Model或ref:Model。 上面的代码是MongoDB中引用子文档的示例,它将给出create()或insertMany()的结果,如下所示:- 这里ObjectId是引用Id或ObjectId(_Id),对于集合中的每个文档都是唯一的。
有关更多信息,请参考ORM对不起,我解决了这个问题,因为我主要关注使用typeorm来完成这项工作,但创建模型的是TypeGoose。 以下代码供解决方案参考:- 模型公司(上级单据) 上面的“ref”是并且从不做ref:()=>Model或ref:Model。 上面的代码是MongoDB中引用子文档的示例,它将给出create()或insertMany()的结果,如下所示:- 这里ObjectId是引用Id或ObjectId(_Id),对于集合中的每个文档都是唯一的。 有关更多信息,请参阅和了解ORM
import { getModelForClass, prop } from '@typegoose/typegoose';
@ObjectType('CompanyType')
@InputType('CompanyInput')
@Entity('company')
class Company{
@Field(() => [Company] || null, { nullable: true })
@prop({
unique: false, required: false,
autocomplete: true,
default: [], ref: 'Branch'
})
public branches?: Ref<Branch>[];
}
import { getModelForClass, prop } from '@typegoose/typegoose';
@ObjectType('BranchType')
@InputType('BranchInput')
@Entity('branch')
class Branch{
@Field(() => Int, { nullable: true })
@prop({
unique: false, required: false,
})
@PrimaryGeneratedColumn('increment')
public id: number;
}
{
branches: [ObjectId("Some pattern"),...]
}