Search code examples
mongodbgrailsgrails-orm

Grails 3 saving mongoDB document with both _id and id


We noticed an issue after an upgrade to grails 3 that we were saving mongoDB documents with both _id and id. (example document below)

How do we stop the saving of id? This happens for every collection the application creates and updates documents for.

{
"_id" : ObjectId("5b0ed1b710b3641a98aaee63"),
"value" : "testing",
"type" : "testingCreate",
"updateDate" : ISODate("2018-05-30T16:30:39.987Z"),
"updateUser" : "TSTUSR",
"id" : ObjectId("5b0ed1b710b3641a98aaee63")
}

The save is being called from the following

def test = new AppParam(type: "testingCreate",
                        updateUser: "TSTUSR", 
                        updateDate: new Date(), 
                        value: "testing")
test.save(failOnError:true, flush:true)

for the appParam domain of

class AppParam {
ObjectId id
String type
String value
String updateUser
Date updateDate

static mapWith = "mongo"

static mapping = {
    version false
    writeConcern WriteConcern.ACKNOWLEDGED
}

static constraints = {
    type size: 1..50, matches:/^[^<>]{1,50}$/, validator: { field, obj ->
        if (!field.trim()) return ['typeRequired']
        return true
    }
    value size: 1..2000, matches:/^[^<>]{1,2000}$/, validator: { field, obj ->
        if (!field.trim()) return ['valueRequired']
        return true
    }

}
}

We are using grailsVersion 3.2.11 and gormVersion 6.1.7.RELEASE


Solution

  • Did a bit more research into Mike W's comment after Grails 3.X it should be defaulting the mongodb engine to codec and we were manually defaulting the mongodb.engine = "mapping".