gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:48:34 24 4
gpt4 key购买 nike

升级到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
}

关于mongodb - Grails 3使用_id和id保存mongoDB文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50610401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com