node.js - 如何使用 mongodb 命令删除字段内的 html 标签?

标签 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。

最佳答案

从你的标签来看,我假设你正在使用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();
  });
});

希望对你有帮助!

关于node.js - 如何使用 mongodb 命令删除字段内的 html 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42855951/

相关文章:

node.js - 使用自定义源代码替换默认的 Express/Connect JS 文件

node.js - Azure Functions + node.js - 找不到模块

python - 将大型收藏分成较小的收藏?

javascript - 如何在MongoDB中存储和区分数据?

mongodb - Azure MongoDB + Synapse 链接 : Column values unexpectedly contain JSON

node.js - 无法将嵌套 json 插入 cassandra

node.js - 带有 Passport 的 Express Flash 消息不起作用

javascript - 使用 Node.js EventEmitter 而不使用子类化

JavaScript 变量 poof 在 findById 回调中不存在

node.js - 如何在Hapi.Js中获取完整的url?