私はmongo DBと対話するGrailsプロジェクトを持っています。ネストされたデータを表すドメイン クラスを作成するためのベスト プラクティスを知りたいです。
データの例:
settings:{
user:{id:1234 , name:"john"}
colors:{header:"red" , footer:"white"}
}
ヘルプやコード例をいただければ幸いです
私はmongo DBと対話するGrailsプロジェクトを持っています。ネストされたデータを表すドメイン クラスを作成するためのベスト プラクティスを知りたいです。
データの例:
settings:{
user:{id:1234 , name:"john"}
colors:{header:"red" , footer:"white"}
}
ヘルプやコード例をいただければ幸いです
mongodb プラグインを使用していると推測するのが妥当だと思います。
簡単に言えば:
Class Settings {
int user_id
String user_name
String colors_header
String colors_footer
}
あなたが提示したようなレガシーmongodbコレクションがある場合:
Class User {
int id
String name
}
Class Color {
String header
String footer
}
Class Settings{
User user
Colors colors
static embedded = ['user','colors']
}
MongoDB は完全にスキーマレスであるため、リレーショナル データベースのように固定数の列に制限されません。そのため、ネスティング データを作成するのはかなり簡単です。
以下に例を示します。
//the domain class
class Settings {
Map user
Map colors
}
//in the groovy controller
def s = new Settings(user: [name:"jhon"],colors:[header:"red" ,footer:"white"] )
s.save(flush: true)