使用MongoDB的Grails不保存空值

时间:2017-12-24 11:03:14

标签: mongodb grails gorm

这是Grails(v3.1)中的客户域类:

class Customer {

   static mapWith = 'mongo'

   String id
   String email
   Boolean blacklisted
   String name
   String telephone

   Date dateCreated
   Date lastUpdated

   String language = 'en'

   static constraints = {
       email nullable: false
       blacklisted nullable: false
       name nullable: true
       language nullable: true
       telephone nullable: true
   }

   static mapping = {
       version false
   }
}

我可以使用这个类插入并更新到MongoDB中的Customer集合,它运行正常。当我尝试使用空值保存其中一个字段时会出现此问题。

customer.telephone = null
customer.save()

将值设置为null对MongoDB集合中的字段没有影响,其值将保持设置为更新前的值。例如,如果电话设置为“1234567”,当我将其更新为null时,MongoDB中的值仍为“1234567”。

我尝试在failOnError: true中使用flush: truesave(),两者都无效。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试直接使用mongo驱动程序查看它是mongo问题还是Gorm问题。

Customer.collection.updateOne([_id:'the-id'],[telephone:null])