4
public class Address {
    static mapWith = "mongo"

    Region region;
    Zone zone;

    static mapping = {
        id generator: 'identity'
        region reference:true
        zone reference:true
    }
}

I'm interested in knowing what reference:true does.

In my experience, leaving it off gives exactly the same result except there's no DBRef in the actual mongo document.

Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90

2 Answers2

2

It looks like reference controlls how documents are linked.

When true, the related documents are referenced by db-refs, if false, GORM inserts simple id, aka Manual references in mongo

injecteer
  • 20,038
  • 4
  • 45
  • 89
  • Thanks injecteer! Is there any benefits to having documents referenced by db-refs with GORM? The manual says "Unless you have a compelling reason to use DBRefs, use manual references instead." – Alexander Suraphel Jun 10 '15 at 11:30
  • from the very same docu: `By including these names, DBRefs allow documents located in multiple collections to be more easily linked with documents from a single collection`. so, if you need to link docs from a single collection, manual refs are just fine – injecteer Jun 10 '15 at 11:33
  • But that information can be found at the domain class definition. So GORM knows which collection to look for by seeing the class of the field, right? – Alexander Suraphel Jun 10 '15 at 11:36
  • In general, both options are working, but I think that db-ref might be better optimized – injecteer Jun 10 '15 at 11:41
  • 1
    @AlexanderSuraphel Also have a look at this [thread](http://stackoverflow.com/questions/9412341/mongodb-is-dbref-necessary) for why using references in mongodb is not a good idea – Merhawi Fissehaye Sep 04 '15 at 06:50
1

This means that those properties will be stored on your Address record by reference. The id for Region and the id for Zone will exist on the record when you query the database instead of storing the entire object's mapping and any objects that its mapping may contain. Returning the Address object would look something like this:

{
  "id": "2413",
  "region": DBRef("region", "1234"),
  "zone": DBRef("zone", "4321")
}

For non-embedded associations by default GORM for MongoDB will map links between documents using MongoDB database references also known as DBRefs. If you prefer not to use DBRefs then you tell GORM to use direct links by using the reference:false mapping.

Gorm Mapping
Searchable Reference

dspano
  • 1,540
  • 13
  • 25
  • The link that you gave is about `Searchable` which I did not mention. – Alexander Suraphel May 14 '15 at 12:13
  • I updated the answer to show a link to more relevant content. The Searchable reference uses the reference:true mapping the same as Gorm Mapping. I thought that the concept could be derived from that searchable link, regardless the answer is the same. – dspano May 27 '15 at 19:47