mongodb+spring boot如何修改深层嵌套对象

发布时间:2021-09-26 14:47:32 作者:柒染
来源:亿速云 阅读:303

mongodb+spring boot如何修改深层嵌套对象,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

在最近的工作中使用到对mongodb深层嵌套对象的修改,研究了很久,故做此笔记。
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<TopicModel> topicModels = mongoTemplate.find(query, TopicModel.class);
        for (int i = 0; i < topicModels.size(); i++) {
            if (topicModels.get(i).getId().equals(tmId)) {
                List<TopicTableModel> topicTableModels = topicModels.get(i).getTableList();
                for (int j = 0; j < topicTableModels.size(); j++) {
                    if (topicTableModels.get(j).getTableId().equals(tbId)) {
                        List<TopicColumnModel> 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;
    }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 关于mongodb的简介
  2. 连接mongodb数据库的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mongodb spring boot

上一篇:ubuntu12.04安装mongodb+eclipse erlang plugin+erlang runtime的操作方法

下一篇:如何做到设计感和内容度都很饱满

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》