简体   繁体   English

我在 prisma 中使用 nest js 和 mongodb,我想添加三个集合,我该怎么做?

[英]i'm using nest js and mongodb with prisma and i want to add three collection in how can i do?

i want to add three model of prisma just like.我想添加三个 model 的 prisma 就像。 schema.prisma模式.prisma

model women{
id  string @id @default(auto()) @map("_id") @db.ObjectId
womenname: string;
womengender: string
womendob: string
womenincome: string
}
model men{
id  string @id @default(auto()) @map("_id") @db.ObjectId
malename: string;
malegender: string
maledob: string
maleincome: string
}
model child{
id  string @id @default(auto()) @map("_id") @db.ObjectId
childname: string;
childgender: string
childdob: string
}

how can i add this three model in one model. just Like this.我如何将这三个 model 添加到一个 model 中。就像这样。

model information{
male: //male model data,
women: // women model data,
child: //child model data
} 

That's already described in the documentation of Prisma, you have two options Prisma 的文档中已经对此进行了描述,您有两种选择

  • Use the composite types so you have separate documents for each entity and you can access them by the parent model like users.tasks使用复合类型,因此每个实体都有单独的文档,您可以通过父 model 访问它们,例如 users.tasks
model User {
  id     String  @id @default(auto()) @map("_id") @db.ObjectId
  tasks  Task[]
}

model Task {
  id              String   @id @default(auto()) @map("_id") @db.ObjectId
  user            Product  @relation(fields: [userId], references: [id])
  userId          String   @db.ObjectId
}
  • Or if you just want to have the interface (a type that defines the attributes that a property in your collection can have) you can just define a custom type like the following, taking the same example of the user and the tasks或者,如果您只想拥有接口(一种定义集合中的属性可以具有的属性的类型),您可以像下面这样定义一个自定义类型,以用户和任务的相同示例为例
model User {
  id   String @id @default(auto()) @map("_id") @db.ObjectId
  tasks Task[]
}

// This will not be a different collection but it will work as "rules" or schema for the attributes inside the user.tasks property

type Task {
  title       String
  description String
}

With this last example you will have something like this使用最后一个示例,您将拥有类似的东西


// users document
{
  "_id": "SOME RANDOM ID",
  "tasks": [
    {
      "title": "SOME TITLE",
      "description": "SOME DESCRIPTION"
    }

  ]
}

You can also try to take a deeper look at the documentation here https://www.prisma.io/docs/concepts/components/prisma-client/composite-types您也可以尝试在此处更深入地查看文档https://www.prisma.io/docs/concepts/components/prisma-client/composite-types

I hope it has been helpful.我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在我用 three.js 安装的 obj 扩展 model 的某些部分添加一个热点。 我该怎么做? - I want to add a hotspot to certain parts of the obj extension model I have installed with three.js. how can I do it? 如何使用JS / JSON对象更新MongoDB集合? - How can I update a MongoDB collection using a JS/JSON object? 如何使用 mongoDB 更新 prisma 中已存储的布尔值? - How can I update an already stored boolean value in prisma with mongoDB? 如何在Node.js中的嵌套mongodb集合中添加和删除? - How can I add and remove to / from a nested mongodb collection in Node.js? 如何通过单击前端按钮在 mongoDB 上添加收藏? - How can I add Collection on mongoDB by clicking button on frontend? 如何使用monk,mongodb和node.js访问return集合 - How do I access the return collection using monk, mongodb, and node.js 如何使用express / mongoose和客户端JS将HTML类发布到mongoDB集合中? - How do I post an HTML class into a mongoDB collection using express/mongoose and client-side JS? 当我使用 react.js 单击选择时,如何显示三个参数? - How can I do to show three params when I click on the select using react.js? 如何在three.js中为三角形添加纹理? - how do I add a texture to a triangle in three.js? 如何在three.js中为ShaderMaterial添加颜色? - How do I add color to ShaderMaterial in three.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM