[英]Best approach to make many to many relation in MongoDB
I have two models for MongoDB我有两个型号 MongoDB
Musician and Album音乐家和专辑
A music album can be sung/played by one or more musicians.一个音乐专辑可以由一个或多个音乐家演唱/播放。
A musician can contribute (sing or play) to multiple music albums.音乐家可以为多张音乐专辑做出贡献(唱歌或演奏)。
Music album model:音乐专辑 model:
albumName: {
type: String
},
releaseDate: {
type: Date
},
genre: {
type: String
},
price: {
type: Number
},
description: {
type: String
},
songs: {
type: Array
}
Musician model:音乐家 model:
artistName: {
type: String
},
artist_type: {
type: String
}
Can you suggest a way to implement a many-to-many relationship in MongoDB?你能建议一种在 MongoDB 中实现多对多关系的方法吗?
thank you谢谢你
As MongoDB is a NoSql database, It's inconvenient to expect ORM functionalities from it.由于 MongoDB 是 NoSql 数据库,因此期望从中获得 ORM 功能是不方便的。
You can implement it using a different way, though.不过,您可以使用不同的方式来实现它。
You can save a musician
model's array of objects into a music album model like the following schema.您可以将
musician
模型的对象数组保存到音乐专辑 model 中,如下所示。
albumName: {
type: String
},
releaseDate: {
type: Date
},
genre: {
type: String
},
price: {
type: Number
},
description: {
type: String
},
songs: {
type: Array
},
musician : {
type: Array
}
where musician field will look like:音乐家领域将如下所示:
[
{
"artistName" : "Swift taylor",
"artist_type" : "english"
},
{
"artistName" : "Justin Bieber",
"artist_type" : "english"
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.