mongodb - Grails MongoDB 列表不更新,我做错了什么吗?

标签 mongodb grails grails-orm

我目前正在使用带有 MongoDB 插件 (org.grails.plugins:mongodb:6.0.0.RC1) 的 Grails 2.5.4,每当我尝试更新任何域类的列表时,它都不起作用,例如:

投票等级:

class Votation {

String question
int minVotes
List <VoteOption> options
User owner
Chat chat

static belongsTo = [chat: Chat]
static embedded = ['options']

static constraints = {
    owner nullable: false
    chat nullable: false
    //question nullable: false
}

投票选项类:
class VoteOption {
   String option
   String url
   List <User> voters

   static belongsTo = [chat: Chat]
}

当我尝试更新列表时:
 //some more code...
 Votation votation = Votation.findById(votationId as Long)
 VoteOption option = votation.options.find { it.option == votationOption }
 User user = User.findOrCreateNew(params.user)
 if (option.voters) {
     option.voters.add(user) // THIS DOESN'T WORK!
 }
 else {
     option.voters = [user] //This DOES work
 }

这只是一个例子,我还有 2 个域类也有列表,它们也不起作用。
重新启动 Grails 并不能解决这个问题,而且这也发生在其他开发人员的计算机上,所以这不是我的环境。其他所有内容都正确保存

最佳答案

Try this

 //some more code...
 Votation votation = Votation.findById(votationId as Long)
 VoteOption option = votation.options.find { it.option == votationOption }
 User user = User.findOrCreateNew(params.user)
 if (user) {
     option.addToVoters(user) // <----
 }

 option.save(flush:true, failOnError:true)

引用:http://docs.grails.org/2.1.0/ref/Domain%20Classes/addTo.html

关于mongodb - Grails MongoDB 列表不更新,我做错了什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39967012/

相关文章:

hibernate - Grails java.lang.IllegalStateException : Method on class [] was used outside of a Grails application

hibernate - 具有null属性的Hibernate OR条件

grails - 如何在 Grails 2.4.4 的域类中使用 transient 值

mysql - 如何使事务在 Grails 中工作

mongodb - 无法连接到 Kubernetes 集群中的 mongodb 服务

html - Grails上的 Assets 管道

node.js - Mongodb(mongolab)Node.js 在集合中找不到任何东西

grails - Grails GORM 查询中 join 的使用

php - 无法安装,当前用户无法写入 channel "pecl.php.net"的 php_dir

mongodb - 为什么我的 mongo 集合在 azure ubuntu 实例上被删除?