加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 服务器 > 系统 > 正文

咋用mongodb和spring boot修改深层嵌套对象

发布时间:2023-10-05 15:09:21 所属栏目:系统 来源:
导读:如何用mongodb和spring boot修改深层嵌套对象?这篇文章运用了实例代码展示,代码非常详细,可供感兴趣的小伙伴们参考借鉴,希望对大家有所帮助。

1.开发环境:mongodb+spring boot项目,使用mongoTemplate进行修
如何用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<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;
 
    }
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章