如何使用mongodb命令删除字段内的html tage?

时间:2017-03-17 11:09:23

标签: node.js mongodb express

我有一个像这样存储的测试和数据集合

  {
    "_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"
}

如果您在文档中看到InfoHtml字段,它包含html标签,我想删除所有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 个答案:

答案 0 :(得分: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();
  });
});

希望它有所帮助!

答案 1 :(得分:1)

不,不是在MongoDB中。

MongoDB没有提供很多处理字段值的功能;它旨在返回字段值,并让客户端应用程序进行进一步处理;例如Ankit's answer中建议的那样。

对于字符串字段,有a small number of string processing functions available,例如$ split,但没有什么比将html转换为纯文本更复杂。