Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails MongoDB Plugin: Embedded Collections vs References

Tags:

mongodb

grails

Originally posted on the Grails mailing list...

I'm not generally a database guy but with the release of the latest mongodb plugin for grails, I wanted to see what the big deal is with noSQL databases. MongoDB seems pretty interesting. I was reading the information on Document oriented storage and came across the following scenario:

Customer / Order / Order Line Item

The doc says orders should be a collection. customers a collection. line-items should be an array of line-items embedded in theorder object.

With regards to GORM, how is this pattern ensured? I'd typically have the following:

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

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

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

How do I make sure that Orders is its own collection and not embedded within Customer? If that is the default, how to I make sure that OrderItems are embedded in Order and not it's own collection? What is the default here?

Thanks.

like image 902
Gregg Avatar asked Nov 19 '10 02:11

Gregg


1 Answers

From reading the Grails MongoDB Plugin documentation, it seems like you need to specifically declare embedded objects, with references being the default.

With that in mind, if you want to make sure that Orders to be its own collection, and OrderItems to be embedded, try this:

class Customer {
  List<Order> orders
}

class Order {
  List<OrderItem> orderItems
  static embedded = [ 'orderItems' ]
}

class OrderItems {
  // properties go here.   
}

Here's the documentation.

like image 134
Pat Avatar answered Sep 21 '22 11:09

Pat