我有两个用于MongoDB的模型
音乐家和专辑
一张音乐专辑可以由一个或多个音乐家演唱/演奏。
一位音乐家可以为多张音乐专辑作贡献(唱歌或演奏)。
音乐专辑模型:
albumName: {
type: String
},
releaseDate: {
type: Date
},
genre: {
type: String
},
price: {
type: Number
},
description: {
type: String
},
songs: {
type: Array
}
音乐家模型:
artistName: {
type: String
},
artist_type: {
type: String
}
你能推荐一种在MongoDB中实现多对多关系的方法吗?
谢谢
发布于 2021-06-10 15:16:19
由于MongoDB是一个NoSql数据库,因此很难期望它具有对象关系映射功能。
不过,您可以使用不同的方式来实现它。
您可以将musician
模型的对象数组保存到音乐专辑模型中,如以下模式所示。
albumName: {
type: String
},
releaseDate: {
type: Date
},
genre: {
type: String
},
price: {
type: Number
},
description: {
type: String
},
songs: {
type: Array
},
musician : {
type: Array
}
其中音乐家字段将如下所示:
[
{
"artistName" : "Swift taylor",
"artist_type" : "english"
},
{
"artistName" : "Justin Bieber",
"artist_type" : "english"
}
]
https://stackoverflow.com/questions/67914760
复制相似问题