Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB, Grails, and relationships

I was curious about how the MongoDB plugin for Grails would handle relationships. In order to test this I made a very simple application with two domain classes:

Authors have two fields: String firstName and String lastName

Books have two fields: String title and Author author

After setting up MongoDB and Grails I made some Authors and Books and took a peek using the MongoDB interactive shell. What I found is that the relationships were being handled the same way they would be handled in a relational database: references to other objects' id fields.

So now for the questions:

  1. In order for GORM to pull this off, does it need a separate connection to retrieve each document?
  2. If yes, wouldn't this be better off in a relational database such as PostgreSQL or MySQL?
  3. If the answer to the above two questions is indeed 'yes,' then is there a better way to manage relationships in a document database such as MongoDB? I realize MongoDB isn't supposed to be relational, but there are some things that I don't see how to get around relationships without duplicating data (thereby making update nightmares).

Edit: I also just noticed that grails is not sorting properly on the 'id' property of my authors. Does this have to do with using MongoDB? In the shell I can see that the _id property of all the documents made by Grails is of the datatype NumberLong.

like image 851
knpwrs Avatar asked Oct 12 '22 14:10

knpwrs


2 Answers

I realize MongoDB isn't supposed to be relational, but there are some things that I don't see how to get around relationships without duplicating data

Then don't sweat it. MongoDB is not anti-relational, it's document-oriented.

In this case, Books and Authors are two top-level objects. It's not reasonable to nest either of them, they are both core entities in their own right.

In the case of each Book having only one Author (N:1), it's completely reasonable for the Book to contain a "Reference To" the Author. Sure you'll have to do two queries. But is that terribly different from doing a join query? The join query still has to do two index look-ups and two data lookups. So you're not really costing yourself anything here.

In the case of each Book supporting multiple Authors (M:N), then you have several options based on your needs.

I don't like to think of MongoDB as "not relational", I think it's cleaner to think of MongoDB as query-optimized.

I also just noticed that grails is not sorting properly on the 'id' property of my authors...

I would check directly with the Grails author. Sounds like they may be storing "strings" instead of actual ObjectIds (or MongoIDs). While not critical this may be a bug.

like image 177
Gates VP Avatar answered Nov 09 '22 14:11

Gates VP


In regards to the id property, the documentation now shows that you can put a declaration of ObjectId id or String id in your domain class in order to not use the default GORM implementation of using an iterating long. Simply declare the field in your class, and the plugin will take care of the rest.

like image 22
bksaville Avatar answered Nov 09 '22 14:11

bksaville