mongodb - 使用MongoDB的Grails无法保存空值

标签 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(),但都无法正常工作。有什么建议么?

最佳答案

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

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

关于mongodb - 使用MongoDB的Grails无法保存空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47960011/

相关文章:

windows - Windows上的Meteor MongoDb连接

grails - "Groovy:unable to resolve class ..."(LoginController.groovy 中的导入错误)

hibernate - 看似随机1 = 1由GORM的where闭包生成的where子句

node.js - 调用 findOne 和填充时转换为 objectid 失败

C# Mongodb 将 List<BsonDocument> 合并为单个 BsonArray

python - 如何快速获取所有文档 MongoDB pymongo

hibernate - hibernate 关联缓存

Grails - 查询域实例的属性

grails - Grails jQuery Mobile 应用程序中的 Spring Security 刷新错误

grails - GORM findAll不起作用