網(wǎng)站首頁 編程語言 正文
文檔結(jié)構(gòu):
{
"_id": ObjectId("9683839960905afb31542868"),
"articleDetail": "<p><strong>尊敬的用戶:</strong></p>\n<p> 根據(jù)XXX通知精神,通知如下圖:</p>\n<img src='https://www.oss.main.com/upload/file/20211221/D1E15045BA504614BCB9CF6D2DBBF147.png' />\n<p style=\"text-align: right;\"><strong> 特此公告!</strong></p>",
"author": "user001",
"categoryId": "1",
"clickCount": NumberLong("1"),
"enable": NumberInt("1"),
"files": [ ],
"images": [
{
"urlType": NumberInt("0"),
"url": "https://www.oss.main.com/upload/file/20211221/D1E15045BA504614BCB9CF6D2DBBF147.png",
"name": "img001.png",
"suffix": "png"
},
{
"urlType": NumberInt("0"),
"url": "https://www.oss.main.com/upload/file/20211221/D1E15045BA504614BCB9CF6D2DBBF147.png",
"name": "img002.png",
"suffix": "png"
}
],
"isDelete": NumberInt("0"),
"link": null,
"sort": NumberInt("0"),
"source": "無",
"summary": "摘要",
"title": "這是一個標(biāo)題",
"type": NumberInt("0"),
"updateTime": NumberLong("1636568109000")
}
MongoDB更新語句:
// 功能描述:將數(shù)據(jù)庫中的URL進行替換
var result= "https://www.baidu.com";
// 文章修改
// 這里的find查詢出了所有文檔,實際運用中為了效率可以過濾一部分內(nèi)容
db.article.find().forEach(
function(item) {
// 修改內(nèi)容里的URL(單個字段)
var detail = item.articleDetail;
if (detail != null) {
print("detail start change:" + item._id);
detail = detail.toString().replace(/http[s]*:\/\/[^\/]*/gi, result);
// 使用update進行修改,可兼容MongoDB 3.x
db.article.update({"_id":item._id}, {$set:{"articleDetail": detail}});
}
// 遍歷圖片數(shù)組,進行修改(數(shù)組遍歷修改)
if (item.images != null) {
item.images.forEach(
function(arr, index) {
var url = item.images[index].url.toString();
print('old:' + url);
url = url.replace(/http[s]*:\/\/[^\/]*/gi, result);
print("new:" + url);
// 拼接數(shù)組下標(biāo)
var tmp = "images."+index+".url";
// 構(gòu)建一個JSON對象,傳入update
tmp = JSON.parse('{"$set":{"'+ tmp + '" : "'+url+'"}}');
db.article.update({"_id":item._id}, tmp);
}
);
}
}
);
原文鏈接:https://blog.csdn.net/a272265225/article/details/122314405
相關(guān)推薦
- 2023-01-02 Python中字符串的常用方法總結(jié)_python
- 2022-05-16 C語言中有哪些字符處理函數(shù)你知道嗎_C 語言
- 2022-04-03 Pandas搭配lambda組合使用詳解_python
- 2022-07-14 React?Native?Popup實現(xiàn)示例_React
- 2022-04-07 C++11實現(xiàn)字符串分割的示例_C 語言
- 2022-07-10 ENOENT讀取文件報錯(fs)
- 2022-10-07 Android實現(xiàn)點擊圖片上傳SQLite數(shù)據(jù)庫_Android
- 2022-07-24 C語言超詳細i講解雙向鏈表_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支