Home > OS >  Mongoose indexing html text?
Mongoose indexing html text?

Time:12-18

I have a doc with field html. The html field stores real 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.

Is there any way to search that document only using text inside of that html tag.

CodePudding user response:

You can use $regex operator:

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

Working example

CodePudding user response:

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.

  • Related