溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何用mongodb和spring boot修改深層嵌套對象

發(fā)布時間:2020-06-05 10:44:05 來源:億速云 閱讀:996 作者:Leah 欄目:MongoDB數據庫

如何用mongodb和spring boot修改深層嵌套對象?這篇文章運用了實例代碼展示,代碼非常詳細,可供感興趣的小伙伴們參考借鑒,希望對大家有所幫助。

1.開發(fā)環(huán)境: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;
    }

看完這篇文章,你們學會用mongodb和spring boot修改深層嵌套對象了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI