Get the App
SLTechnology News&Howtos  ›  Database  › 

How mongodb+spring boot modifies deeply nested objects

Shulou Source: shulou.com Published: 2022-05-31 17:36:46 10月03日 Update

Mongodb+spring boot how to modify deeply nested objects, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Changes to deeply nested objects in mongodb have been used in recent work and have been studied for a long time, so I took this note.

1. Development environment: mongodb+spring boot project, using mongoTemplate for modification

two。 The data is nested in three layers: TopicModel-- > TopicTableModel-- > TopicColumnModel

3. Modify the code display

(1) modify the second-level TopicTableModel object

@ 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) to modify the third level (TopicColumnModel), you need to first traverse the index of the object located to the modified level 3.

@ 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;} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Objects depth help clarity code content for this data articles novices more environment knowledge notes indexes industry information information channels problems needs Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Xiaomi Huawei MariaDB Shulou Technology