我正在尝试在Grails GORM Mongo域类中创建嵌入式集合。
class User {
String name
Set<String> friends = []
}
我要存储其他用户名的Set( 为非重复列表)。
当我尝试保存用户域类时:
new User(name: 'Bob').save(failOnError: true)
我得到了错误。
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for interface java.util.Set.
将Set更改为List可以很好地工作,但我不想重复,也不必使用List来管理。
GORM是否有办法使用底层的Mongo
$addToSet功能。
请您参考如下方法:
可能是GORM MongoDB问题。您可以使用重现问题来创建问题here。
但是现在,您可以使用List这样解决此问题:
class User {
String name
List<String> friends = []
void removeDuplicate() {
this.friends?.unique()
}
def beforeInsert() {
removeDuplicate()
}
def beforeUpdate() {
removeDuplicate()
}
}




