这是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: true
和save()
,但都无法正常工作。有什么建议么?
最佳答案
您可以尝试直接使用mongo驱动程序来查看它是mongo问题还是Gorm问题。
Customer.collection.updateOne([_id:'the-id'],[telephone:null])
关于mongodb - 使用MongoDB的Grails无法保存空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47960011/