【问题标题】:Grails MongoDB Plugin: Embedded Collections vs ReferencesGrails MongoDB 插件:嵌入式集合与引用
【发布时间】:2011-05-12 10:00:10
【问题描述】:

最初发布在 Grails 邮件列表中...

我一般不是数据库专家,但随着 grails 的最新 mongodb 插件的发布,我想看看 noSQL 数据库有什么大不了的。 MongoDB 似乎很有趣。我正在阅读有关面向文档的存储的信息并遇到以下情况:

客户/订单/订单行项目

文档说订单应该是一个集合。客户收藏。 line-items 应该是嵌入在 order 对象中的 line-items 数组。

关于 GORM,如何确保这种模式?我通常有以下内容:

class Customer {
    static hasMany = [orders: Order]
}

class Order {
    static hasMany = [orderItems: OrderItem]
    static belongsTo = [customer:Customer]
}

class OrderItem {
    static belongsTo = [order:Order]
}

我如何确保 Orders 是它自己的集合而不是嵌入到 Customer 中?如果这是默认设置,我如何确保 OrderItems 嵌入 Order 而不是它自己的集合?这里的默认是什么?

谢谢。

【问题讨论】:

    标签: grails mongodb


    【解决方案1】:

    通过阅读 Grails MongoDB 插件文档,您似乎需要专门声明嵌入式对象,引用是默认值。

    考虑到这一点,如果您想确保 Orders 成为自己的集合,并且 OrderItems 被嵌入,请尝试以下操作:

    class Customer {
      List<Order> orders
    }
    
    class Order {
      List<OrderItem> orderItems
      static embedded = [ 'orderItems' ]
    }
    
    class OrderItems {
      // properties go here.   
    }
    

    Here's the documentation.

    【讨论】:

    • 嗨,这个答案很有帮助,但它并没有真正回答问题——它只是提供了寻找答案的方向。具体来说,我仍然不清楚是否会在 mongodb 上下文中使用“hasMany”之类的东西(我猜不是),如果没有,那么在域类中应该如何准确地表示这些集合是正确存储在 mongodb 中。我认为如果明确回答 Gregg 的问题(“我如何确保 Orders ... 嵌入 Order 而不是它自己的集合?这里的默认设置是什么?”),这将是一个很好的学习例子。
    • @mindthief:更新了答案,使其在回答问题时更加明确。另外,我相信“hasMany”更像是一种关系数据库的概念,这也是 MongoDB 不使用它的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 2016-02-23
    • 2012-03-20
    相关资源
    最近更新 更多