mongodb+spring boot如何修改深層嵌套對象,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
在成都做網(wǎng)站、網(wǎng)站設(shè)計過程中,需要針對客戶的行業(yè)特點、產(chǎn)品特性、目標(biāo)受眾和市場情況進(jìn)行定位分析,以確定網(wǎng)站的風(fēng)格、色彩、版式、交互等方面的設(shè)計方向。成都創(chuàng)新互聯(lián)公司還需要根據(jù)客戶的需求進(jìn)行功能模塊的開發(fā)和設(shè)計,包括內(nèi)容管理、前臺展示、用戶權(quán)限管理、數(shù)據(jù)統(tǒng)計和安全保護(hù)等功能。在最近的工作中使用到對mongodb深層嵌套對象的修改,研究了很久,故做此筆記。
1.開發(fā)環(huán)境:mongodb+spring boot項目,使用mongoTemplate進(jìn)行修改
2.數(shù)據(jù)為三層嵌套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(); ListtopicModels = 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; }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。