In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.