新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何用mongodb和springboot修改深層嵌套對象
如何用MongoDB和spring boot修改深層嵌套對象?這篇文章運用了實例代碼展示,代碼非常詳細,可供感興趣的小伙伴們參考借鑒,希望對大家有所幫助。
創(chuàng)新互聯(lián)公司于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目網(wǎng)站制作、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元富裕做網(wǎng)站,已為上家服務,為富裕各地企業(yè)和個人服務,聯(lián)系電話:18982081108
1.開發(fā)環(huán)境:mongodb+spring boot項目,使用mongoTemplate進行修改
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();
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;
}
看完這篇文章,你們學會用mongodb和spring boot修改深層嵌套對象了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀。
名稱欄目:如何用mongodb和springboot修改深層嵌套對象
當前網(wǎng)址:http://www.ef60e0e.cn/article/jdoshs.html