0

私はmongo DBと対話するGrailsプロジェクトを持っています。ネストされたデータを表すドメイン クラスを作成するためのベスト プラクティスを知りたいです。

データの例:

settings:{
  user:{id:1234 , name:"john"}
  colors:{header:"red" , footer:"white"}
}

ヘルプやコード例をいただければ幸いです

4

2 に答える 2

0

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']
}
于 2013-01-01T18:41:04.673 に答える
0

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)
于 2013-01-03T08:25:19.210 に答える