简体   繁体   English

ZCCADCDEDB567ABAE643E15DCF0974E503Z 索引 html 文本?

[英]Mongoose indexing html text?

I have a doc with field html.我有一个带有字段 html 的文档。 The html field stores real html. html 字段存储真正的 html。

schema: {
  html: String
}
doc: {
  "html" : "<p>I am html<p>"
}

And I want to index the html field that when I search "I am html" it should return that doc.我想索引 html 字段,当我搜索“我是 html”时,它应该返回该文档。

Is there any way to search that document only using text inside of that html tag.有没有办法仅使用 html 标记内的文本来搜索该文档。

You can use $regex operator:您可以使用$regex运算符:

db.collection.find({
  "html": {
    "$regex": "I am HTML"
  }
})

Working example工作示例

If you have text index on this collection, you can search as:如果您对此集合有文本索引,则可以搜索为:

db.collection.find({
  $text: {
    $search: "\"I am html\""
  }
})

With double-quotes wrapped, you can search as exact phrase.使用双引号括起来,您可以搜索确切的短语。

You can also search for exact phrases by wrapping them in double-quotes.您还可以通过将它们括在双引号中来搜索确切的短语。 If the $search string includes a phrase and individual terms, text search will only match documents that include the phrase.如果 $search 字符串包含短语和单个术语,则文本搜索将仅匹配包含该短语的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM