如何用mongodb和spring boot修改深层嵌套对象
发表于:2024-10-05 作者:千家信息网编辑
千家信息网最后更新 2024年10月05日,如何用mongodb和spring boot修改深层嵌套对象?这篇文章运用了实例代码展示,代码非常详细,可供感兴趣的小伙伴们参考借鉴,希望对大家有所帮助。1.开发环境:mongodb+spring b
千家信息网最后更新 2024年10月05日如何用mongodb和spring boot修改深层嵌套对象
如何用mongodb和spring boot修改深层嵌套对象?这篇文章运用了实例代码展示,代码非常详细,可供感兴趣的小伙伴们参考借鉴,希望对大家有所帮助。
1.开发环境:mongodb+spring boot项目,使用mongoTemplate进行修改
2.数据为三层嵌套TopicModel-->TopicTableModel-->TopicColumnModel
3.修改代码展示
(1)修改第二级TopicTableModel对象
@Override public boolean updateTableModel(TopicTableModel tableModel) { Query query = new Query(); query.addCriteria(Criteria.where("tableList.tableId").is(tableModel.getTableId())); Update update = new Update().set("tableList.$.tableName", tableModel.getTableName()) .set("tableList.$.tableComment", tableModel.getTableComment()) .set("tableList.$.status", Integer.valueOf(tableModel.getStatus())); // .set("tableList.$.topicCode", tableModel.getTopicCode()); UpdateResult tableUr = this.mongoTemplate.upsert(query, update, TopicModel.class); if ((tableUr.getMatchedCount() > 0L) || (tableUr.getUpsertedId() != null)) { return true; } return false; }
(3)修改第三级(TopicColumnModel),需要先遍历定位到修改的第三级对像的索引
@Override public boolean updateColumnModel(TopicColumnModel topicColumnModel, String tmId, String tbId) { Query query = new Query(); query.addCriteria(Criteria.where("tableList.tableId").is(tbId)); Update update = new Update(); List topicModels = mongoTemplate.find(query, TopicModel.class); for (int i = 0; i < topicModels.size(); i++) { if (topicModels.get(i).getId().equals(tmId)) { List topicTableModels = topicModels.get(i).getTableList(); for (int j = 0; j < topicTableModels.size(); j++) { if (topicTableModels.get(j).getTableId().equals(tbId)) { List topicColumnModels = topicTableModels.get(j).getColList(); for (int k = 0; k < topicColumnModels.size(); k++) { if (topicColumnModels.get(k).getColId().equals(topicColumnModel.getColId())) { update.set("tableList.$.colList." + k + ".colName",topicColumnModel.getColName()) .set("tableList.$.colList."+ k +".desc1", topicColumnModel.getDesc1()) .set("tableList.$.colList." + k +".desc2", topicColumnModel.getDesc2()) .set("tableList.$.colList." + k +".internalMark", topicColumnModel.getInternalMark()) .set("tableList.$.colList." + k +".qualifierMark", topicColumnModel.getQualifierMark()) .set("tableList.$.colList." + k +".chineseName", topicColumnModel.getChineseName()) .set("tableList.$.colList." + k +".dataSource", topicColumnModel.getDataSource()) .set("tableList.$.colList." + k +".getRules", topicColumnModel.getGetRules()) .set("tableList.$.colList." + k +".dataType", topicColumnModel.getDataType()) .set("tableList.$.colList." + k +".dataLength", topicColumnModel.getDataLength()) .set("tableList.$.colList." + k +".pkey", topicColumnModel.isPkey()) .set("tableList.$.colList." + k +".index", topicColumnModel.isIndex()) .set("tableList.$.colList." + k +".nullAble", topicColumnModel.isNullAble()) .set("tableList.$.colList." + k +".unique", topicColumnModel.isUnique()) .set("tableList.$.colList." + k +".colComment", topicColumnModel.getColComment()) .set("tableList.$.colList." + k +".defaultValue", topicColumnModel.getDefaultValue()) .set("tableList.$.colList." + k +".check", topicColumnModel.getCheck()) .set("tableList.$.colList." + k +".attributeType", topicColumnModel.getAttributeType()); } } } } } } UpdateResult tableUr = this.mongoTemplate.updateFirst(query, update, TopicModel.class); if ((tableUr.getMatchedCount() > 0L) || (tableUr.getUpsertedId() != null)) { return true; } return false; }
看完这篇文章,你们学会用mongodb和spring boot修改深层嵌套对象了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读。
对象
代码
深层
更多
篇文章
兴趣
内容
实例
小伙
小伙伴
技能
数据
环境
索引
行业
资讯
资讯频道
项目
频道
j++
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
北京先进软件开发团队
网络安全知识内容青少年
sql数据库删除连接
公安网络安全大会
腾讯云服务器安全
集团网络安全管理方案
windows迁移服务器
dns服务器广东移动
讯达网络技术
数据库技术在农业的应用
网站服务器租用什么公司好
甘肃诚诚网络技术有限公司
无法访问服务器8080
四川省达州市公安局网络安全保卫
qq能不能清空所有数据库
软件开发过程中的重大安全
首选dns哪个服务器好
提供java存取数据库能力的包是
提高网络安全的政治站位
pb与数据库的通讯
介绍服务器必备音乐
wind代理服务器
数据库的字符集应该是什么
对软件开发工具进行审计
网络安全周论坛致辞
nosql数据库设计
国家网络安全宣传周宣传图片
数据库登录档案
aix多台服务器同步时间
狂烈之刃新服务器