Mongodb org.springframework.dao.DataIntegrityViolationException:无法创建字段
Spring引导中Mongodb的更新引发异常,下面给出了代码 例外情况Mongodb org.springframework.dao.DataIntegrityViolationException:无法创建字段,mongodb,criteria,Mongodb,Criteria,Spring引导中Mongodb的更新引发异常,下面给出了代码 例外情况 org.springframework.dao.DataIntegrityViolationException:无法创建字段 更新查询 Query queryTwo = new Query(); queryTwo.addCriteria(Criteria.where("customerDetail.CID_18").exists(true)); Update update = new Update();
org.springframework.dao.DataIntegrityViolationException:无法创建字段
更新查询
Query queryTwo = new Query();
queryTwo.addCriteria(Criteria.where("customerDetail.CID_18").exists(true));
Update update = new Update();
update.set("customerDetail.CID_18.activeDevices", "900");
数据库中的数据如下所示
{
"_id" : "3c02a683-4601-419b-a598-9e9d8ce57fd0",
"profileId" : "308",
"name" : "resellerSent",
"email" : "resellerSent@gmail.com",
"customerDetail" : [
{
"CID_18" : {
"customerId" : NumberLong(18),
"activeDevices" : "0",
"_class" : "com.demo.CustomerDetail"
},
"CID_2196" : {
"customerId" : NumberLong(2196),
"activeDevices" : "5",
"_class" : "com.demo.CustomerDetail"
},
"CID_2197" : {
"customerId" : NumberLong(2197),
"activeDevices" : "0",
"_class" : "com.demo.CustomerDetail"
}
}
],
"_class" : "com.demo.ResellerProfile"
}
我使用的是
spring boot starter data mongodb
,版本:“2.3.1.RELEASE”
customerDetail
是一个数组,因此需要使用位置操作符
Query query=Query.query(Criteria.where("customerDetail.CID_18").exists(true));
Update update=new Update().set("customerDetail.$[].CID_18.activeDevices", "900");
return mongoTemplate.updateFirst(query,update,Users.class);
如果只为位置所有添加$[]
,则更新将应用于满足更新条件的所有对象。如果需要更新特定对象,则可以使用filterary()
和set
参考。如果您的答案对您有帮助,请勾选并向上投票,以帮助寻求此类问题的人