Grails 3 使用 _id 和 id 保存 mongoDB 文档

Grails 3 saving mongoDB document with both _id and id

我们在升级到 grails 3 后注意到一个问题,即我们使用 _idid 保存了 mongoDB 个文档。 (下面的示例文档)

我们如何停止 id 的保存?对于应用程序为其创建和更新文档的每个集合,都会发生这种情况。

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

正在从以下调用保存

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

对于

的 appParam 域
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
    }

}
}

我们使用的是 grailsVersion 3.2.11 和 gormVersion 6.1。7.RELEASE

mapping 闭包中尝试以下操作。

static mapping {
    id column: '_id'
    version false
    writeConcern WriteConcern.ACKNOWLEDGED
}

在 Grails 3.X 之后对 Mike W 的评论进行了更多研究 3.X 它应该将 mongodb 引擎默认为编解码器,而我们手动将 mongodb.engine = "mapping".