如何使用 Spring 在 mongodb 文档的嵌套数组中添加 json? [英] How to add a json in a nested array of a mongodb document using Spring?
问题描述
存储在mongodb中的文档:
<预><代码>{"CNF_SERVICE_ID":"1","SERVICE_CATEGORY":"COMMON_SERVICE",服务":[{"SERVICE_NAME":"身份验证服务",版本":[{"VERSION_NAME":"AuthenticationServiceV6_3","VERSION_NUMBER":"2","VERSION_NOTES":"测试","RELEASE_DATE":"21-02-2020","OBSOLETE_DATE":"21-02-2020","状态":"是",组":[{"GROUP_NAME":"测试组",创建日期":"",NODE_NAMES":["],顾客":[{"CUSTOMER_CONFIG_ID":"4","ACTIVATION_DATE":"21-02-2020","DEACTIVATION_DATE":"21-02-2020",状态":是"}]}]}]}]}现在,我需要将另一个客户 json 添加到上面同一文档中GROUPS"内的数组CUSTOMERS"中.客户 json 将是这样的:
<代码>{"CUSTOMER_CONFIG_ID":"10","ACTIVATION_DATE":"16-03-2020","DEACTIVATION_DATE":"16-03-2021",状态":是"}
我试过这个:
<预><代码>Update update = new Update().push("SERVICES.$.VERSIONS.GROUPS.CUSTOMERS",customerdto);mongoOperations.update(query, update, Myclass.class, "mycollection");但是,我收到异常:org.springframework.data.mongodb.UncategorizedMongoDbException:命令失败,错误 28(PathNotViable):'无法在元素中创建字段 'GROUPS'
<小时>
我能够使用过滤后的位置运算符更新它.以下是我使用的查询:
更新({ "SERVICE_CATEGORY":"COMMON_SERVICE", "SERVICES.SERVICE_NAME":"身份验证服务", "SERVICES.VERSIONS.VERSION_NAME":"AuthenticationServiceV6_3"},{ $push:{"SERVICES.$[].VERSIONS.$[].GROUPS.$[].CUSTOMERS": { "CUSTOMER_CONFIG_ID":"6", "ACTIVATION_DATE":"31-03-2020", "STATUS":"Y" } } });
实际上,无论过滤条件如何,此查询都会更新所有字段.所以.我试过这个,但我面临语法异常.请帮忙.
更新({"SERVICE_CATEGORY":"COMMON_SERVICE"},{"SERVICES.SERVICE_NAME":"身份验证服务"},{"SERVICES.VERSIONS.VERSION_NAME":"AuthenticationServiceV6_3"}{$push:{"SERVICES.$[service].VERSIONS.$[version].GROUPS.$[group].CUSTOMERS":{"CUSTOMER_CONFIG_ID":"6","ACTIVATION_DATE":"31-03-2020",状态":是"}}},{多:真实,arrayFilters: [ { $and:[{ "version.VERSION_NAME": "AuthenticationServiceV6_3"},{"service.SERVICE_NAME":"Authentication Service"},{"group.GROUP_NAME":"TEST GROUP"}]}]});
更新:2020 年 4 月 1 日
我试过的代码:
validationquery.addCriteria(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("SERVICES.SERVICE_NAME").is(servicedetail.getService_name()).and("SERVICES.VERSIONS.VERSION_NAME").is(version.getVersion_name()));Update update=new Update().push("SERVICES.$[s].VERSIONS.$[v].GROUPS.$[].CUSTOMERS", customer).filterArray(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("s.SERVICE_NAME").is(servicedetail.getService_name()).and("v.VERSION_NAME").is(version.getVersion_name()));mongoOperations.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);
抛出以下异常:
错误 com.sample.amt.mongoTemplate.MongoOperations - count(query, collectionName,key,env) 中的异常 :: org.springframework.dao.DataIntegrityViolationException: 解析数组过滤器时出错 :: 由 :: 导致 :: 预期单个顶部- 级别字段名称,找到SERVICE_CATEGORY"和s";嵌套异常是 com.mongodb.MongoWriteException: Error parsing array filter :: 由 :: 预期一个顶级字段名称,找到SERVICE_CATEGORY"和s"
感谢@prasad_ 提供查询.我最终能够使用 Spring 数据 MongoTemplate 的 updateMulti 方法成功地将查询转换为代码.我已经发布了以下代码:
Query validationquery = new Query();validationquery.addCriteria(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("SERVICES.SERVICE_NAME").is(servicedetail.getService_name()).and("SERVICES.VERSIONS.VERSION_NAME").is(version.getVersion_name()));Update update=new Update().push("SERVICES.$[s].VERSIONS.$[v].GROUPS.$[].CUSTOMERS", customer).filterArray(Criteria.where("s.SERVICE_NAME").is(servicedetail.getService_name())).filterArray(Criteria.where("v.VERSION_NAME").is(version.getVersion_name()));mongoOperations.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);mongoTemplateobj.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);
Document stored in mongodb:
{
"CNF_SERVICE_ID":"1",
"SERVICE_CATEGORY":"COMMON_SERVICE",
"SERVICES":[{
"SERVICE_NAME":"Authentication Service",
"VERSIONS":[{
"VERSION_NAME":"AuthenticationServiceV6_3",
"VERSION_NUMBER":"2",
"VERSION_NOTES":"test",
"RELEASE_DATE":"21-02-2020",
"OBSOLETE_DATE":"21-02-2020",
"STATUS":"Y",
"GROUPS":[{
"GROUP_NAME":"TEST GROUP",
"CREATED_DATE":"",
"NODE_NAMES":[
""
],
"CUSTOMERS":[{
"CUSTOMER_CONFIG_ID":"4",
"ACTIVATION_DATE":"21-02-2020",
"DEACTIVATION_DATE":"21-02-2020",
"STATUS":"Y"
}]
}]
}]
}
]
}
Now, I need to add another customer json to the array "CUSTOMERS" inside "GROUPS" in the same document above. The customer json would be like this:
{
"CUSTOMER_CONFIG_ID":"10",
"ACTIVATION_DATE":"16-03-2020",
"DEACTIVATION_DATE":"16-03-2021",
"STATUS":"Y"
}
I tried this:
Update update = new Update().push("SERVICES.$.VERSIONS.GROUPS.CUSTOMERS",customerdto);
mongoOperations.update(query, update, Myclass.class, "mycollection");
But, I am getting the exception: org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 28 (PathNotViable): 'Cannot create field 'GROUPS' in element
[ EDIT ADD ]
I was able to update it using the filtered positional operator. Below is the query I used:
update(
{ "SERVICE_CATEGORY":"COMMON_SERVICE", "SERVICES.SERVICE_NAME":"Authentication Service", "SERVICES.VERSIONS.VERSION_NAME":"AuthenticationServiceV6_3"},
{ $push:{"SERVICES.$[].VERSIONS.$[].GROUPS.$[].CUSTOMERS": { "CUSTOMER_CONFIG_ID":"6", "ACTIVATION_DATE":"31-03-2020", "STATUS":"Y" } } }
);
Actually, this query updated all the fields irrespective of the filter conditions. So. I tried this but I am facing syntax exception. Please help.
update(
{"SERVICE_CATEGORY":"COMMON_SERVICE"},
{"SERVICES.SERVICE_NAME":"Authentication Service"},
{"SERVICES.VERSIONS.VERSION_NAME":"AuthenticationServiceV6_3"}
{
$push:{"SERVICES.$[service].VERSIONS.$[version].GROUPS.$[group].CUSTOMERS":{
"CUSTOMER_CONFIG_ID":"6",
"ACTIVATION_DATE":"31-03-2020",
"STATUS":"Y"
}
}
},
{
multi: true,
arrayFilters: [ { $and:[{ "version.VERSION_NAME": "AuthenticationServiceV6_3"},{"service.SERVICE_NAME":"Authentication Service"},{"group.GROUP_NAME":"TEST GROUP"}]} ]
}
);
Update: April 1,2020
The code I tried:
validationquery.addCriteria(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("SERVICES.SERVICE_NAME").is(servicedetail.getService_name()).and("SERVICES.VERSIONS.VERSION_NAME").is(version.getVersion_name()));
Update update=new Update().push("SERVICES.$[s].VERSIONS.$[v].GROUPS.$[].CUSTOMERS", customer).filterArray(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("s.SERVICE_NAME").is(servicedetail.getService_name()).and("v.VERSION_NAME").is(version.getVersion_name()));
mongoOperations.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);
The below exception is thrown:
ERROR com.sample.amt.mongoTemplate.MongoOperations - Exception in count(query, collectionName,key,env) :: org.springframework.dao.DataIntegrityViolationException: Error parsing array filter :: caused by :: Expected a single top-level field name, found 'SERVICE_CATEGORY' and 's'; nested exception is com.mongodb.MongoWriteException: Error parsing array filter :: caused by :: Expected a single top-level field name, found 'SERVICE_CATEGORY' and 's'
Thanks to @prasad_ for providing the query. I was able to eventually convert the query successfully to code with Spring data MongoTemplate's updateMulti method. I have posted the code below:
Query validationquery = new Query();
validationquery.addCriteria(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("SERVICES.SERVICE_NAME").is(servicedetail.getService_name()).and("SERVICES.VERSIONS.VERSION_NAME").is(version.getVersion_name()));
Update update=new Update().push("SERVICES.$[s].VERSIONS.$[v].GROUPS.$[].CUSTOMERS", customer).filterArray(Criteria.where("s.SERVICE_NAME").is(servicedetail.getService_name())).filterArray(Criteria.where("v.VERSION_NAME").is(version.getVersion_name()));
mongoOperations.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);
mongoTemplateobj.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);
这篇关于如何使用 Spring 在 mongodb 文档的嵌套数组中添加 json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!