如何使用mongodb命令删除域内的HTML tage?

我有一个收集testing和数据hase像这样存储

{ "_id": { "$oid": "58cba49d689493500be8d0a5" }, "Latitude": 12.96009039, "Longitude": 77.55213396, "InfoHTML": "<br/>Polling Station No and Name : 131 43 Ward Office (Revenue),BBMP, 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),BBMP, 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),BBMP, 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(); }); }); 

希望能帮助到你!

不,不在MongoDB中。

MongoDB不提供很多处理字段值的function; 它旨在返回字段值,并让客户端应用程序做进一步的处理; 例如Ankit的答案中所build议的 。

在string字段的情况下,可以使用less量的string处理函数 ,例如$ split,但没有像将html转换为纯文本那么复杂。