簡體   English   中英

刪除 HTML 標簽 MondoDB

[英]Remove HTML Tags MondoDB

我正在創建一個查詢來提取 mongodb 中客戶的描述。 不幸的是,描述是 HTML 格式的。 有沒有辦法替換所有 HTML 標簽並將其設為“”。 將其替換為“”或刪除 HTML 標簽。

下面是一個示例文檔

{ 
        "_id" : ObjectId("61f72aefdc85500a8baa6bb8")
        "CustomerPin" : "22010871", 
        "CustomerName" : "TestLastName, TestFirstName", 
        "Age" : 39.0, 
        "Gender" : "Male", 
        "Description" : "<p><span>This will be a test description</span><br/></p>", 
}

輸出應刪除“p”、“span”和“br”。 mongodb 中是否有一個功能可以一次刪除它們而不重復 $project

這是預期的輸出:

{ 
        "_id" : ObjectId("61f72aefdc85500a8baa6bb8")
        "CustomerPin" : "22010871", 
        "CustomerName" : "TestLastName, TestFirstName", 
        "Age" : 39.0, 
        "Gender" : "Male", 
        "Description" : "This will be a test description", 
}

謝謝!

一種方法是在保存方法的預掛鈎中通過正則表達式刪除所有標簽

Description.replace(/(<([^>]+)>)/gi, "");

在這里查看鈎子

如果您使用 Mongo 4.2,那么您必須找到將從 HTML 中提取內容的確切正則表達式。 您還可以在下面找到聚合管道和正則表達式。

db.getCollection("name_of_your_collection").aggregate({
    $set: {
        contentRegex: {
            $regexFind: { input: "$Description", regex: /([^<>]+)(?!([^<]+)?>)/gi }
        }
    }
},
    {
        $set: {
            content: { $ifNull: ["$contentRegex.match", "$Description"] }
        }
    },
    {
        $unset: [ "contentRegex" ]
    }
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM