我有一个与 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"}
}
将不胜感激任何帮助或代码示例
I think it is reasonable to presume that you are using mongodb plugin.
To put it simple:
Class Settings {
    int user_id
    String user_name
    String colors_header
    String colors_footer
}
If there is a legacy mongodb collection like you presented:
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)