2017-03-17 41 views
1

我有存儲這樣如何使用mongodb命令刪除域內的html tage?

{ 
    "_id": { 
     "$oid": "58cba49d689493500be8d0a5" 
    }, 
    "Latitude": 12.96009039, 
    "Longitude": 77.55213396, 
    "InfoHTML": "<br/>Polling Station No and Name : 131 43 Ward Office (Revenue),B B M P, Room No-01 <br/><br/><a href='http://psleci.nic.in/pslinfoc.aspx?S=S10&A=168&P=131 ' target='_blank'><b>Click here for information<b><\/a>", 
    "state": "karnataka", 
    "district": "bbmpcentral", 
    "constituency": "chamrajpet" 
}, 
{ 
    "_id": { 
     "$oid": "58cba645734d1d2ca8573b20" 
    }, 
    "Latitude": 12.96001673, 
    "Longitude": 77.55207344, 
    "InfoHTML": "<br/>Polling Station No and Name : 132 43 Ward Office (Revenue),B B M P, Room No-02 <br/><br/><a href='http://psleci.nic.in/pslinfoc.aspx?S=S10&A=168&P=132 ' target='_blank'><b>Click here for information<b><\/a>", 
    "state": "karnataka", 
    "district": "bbmpcentral", 
    "constituency": "chamrajpet" 
}, 
{ 
    "_id": { 
     "$oid": "58cbaa4d734d1d2ca8573c9b" 
    }, 
    "Latitude": 12.96519429, 
    "Longitude": 77.58097308, 
    "InfoHTML": "<br/>Polling Station No and Name : 11 Abbas Khan Womens College, Darga Compound, <br/><br/><a href='http://psleci.nic.in/pslinfoc.aspx?S=S10&A=169&P=11 ' target='_blank'><b>Click here for information<b><\/a>", 
    "state": "karnataka", 
    "district": "bbmpcentral", 
    "constituency": "chickpet" 
} 

集合測試和數據HASE如果你看到它包含HTML標記的文檔中InfoHtml領域我想刪除所有的HTML標籤

"InfoHTML": "<br/>Polling Station No and Name : 131 43 Ward Office (Revenue),B B M P, Room No-01 <br/><br/><a href='http://psleci.nic.in/pslinfoc.aspx?S=S10&A=168&P=131 ' target='_blank'><b>Click here for information<b><\/a>" 

我期望我應該這樣

例如我給每個文檔中獲得InfoHTML

"InfoHTML": "Polling Station No and Name : 11 Abbas Khan Womens College, Darga Compound", 

是否可以刪除html標籤mongodb。

回答

2

從您的標籤,我假設您正在使用nodejs。在這種情況下,請查看npm包 - striptags

如果我不得不做任何你正在嘗試做的,我會做這樣的:

var striptags = require('striptags'); 
var YourCollectionName = require('path-to-your-model'); 

YourCollectionName.find({}, function (err, docs) { 
    if (err) { 
    //handle or throw error 
    } 

    // For all the documents, remove html tags and save 
    docs.forEach(function(doc){ 
    doc.InfoHTML = striptags(doc.InfoHTML); 
    doc.save(); 
    }); 
}); 

希望它能幫助!

+0

感謝它的工作 –

1

不,不在MongoDB中。

MongoDB不提供很多處理字段值的功能;它旨在返回字段值,並讓客戶端應用程序進行進一步處理;例如如Ankit's answer中所建議的那樣。

在字符串字段的情況下,有a small number of string processing functions available,例如$ split,但沒有像將html轉換爲純文本那麼複雜。