| By default in GORM entities are supplied with an integer-based identifier. So for example the following entity: |
| |
| {code} |
| class Person {} |
| {code} |
| |
| Has a property called @id@ of type @java.lang.Long@. In this case GORM for Mongo will generate a sequence based identifier using the technique [described in the Mongo documentation|http://docs.mongodb.org/manual/tutorial/isolate-sequence-of-operations/] on Atomic operations. |
| |
| However, sequence based integer identifiers are not ideal for environments that require [sharding|http://docs.mongodb.org/manual/sharding/] (one of the nicer features of Mongo). Hence it is generally advised to use either String based ids: |
| |
| {code} |
| class Person { |
| String id |
| } |
| {code} |
| |
| Or a native BSON [ObjectId|http://api.mongodb.org/java/current/org/bson/types/ObjectId.html]: |
| |
| {code} |
| import org.bson.types.ObjectId |
| |
| class Person { |
| ObjectId id |
| } |
| {code} |
| |
| BSON @ObjectId@ instances are generated in a similar fashion to @UUIDs@. |
| |
| h4. Assigned Identifiers |
| |
| Note that if you manually assign an identifier, then you will need to use the @insert@ method instead of the @save@ method, otherwise GORM can't work out whether you are trying to achieve an insert or an update. Example: |
| |
| {code} |
| class Person { |
| String id |
| } |
| ... |
| def p = new Person(id:"Fred") |
| // to insert |
| p.insert() |
| // to update |
| p.save() |
| {code} |