Get the App
SLTechnology News&Howtos  ›  Database  › 

How to modify deeply nested objects with mongodb and spring boot

Shulou Source: shulou.com Published: 2022-06-01 17:42:51 10月28日 Update

How do I modify deeply nested objects with mongodb and spring boot? This article uses the example code to show, the code is very detailed, which can be used for reference by interested friends. I hope it will be helpful to you.

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;}

After reading this article, have you learned to modify deeply nested objects with mongodb and spring boot? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel. Thank you for reading.

Tags: Objects code depth more articles interests content examples guys partners skills data environment index industry information information channels projects channels jackers + Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Huawei Shulou Information macOS OPPO Reno