1

I've different results querying a mongo document using regex and text search. If I use regex I obtain results:

db.planning.find({"year":2018, "data":{$regex: ".*#A.*"}})

{ "_id" : ObjectId("5bf2964723eeeb59ef26dc69"), "date" : ISODate("2018-04-10T00:00:00Z"), "month" : 4, "instrument" : "AV500", "day" : 10, "data" : "N2 / Zizic #A (MP)", "year" : 2018 }
{ "_id" : ObjectId("5bf2964823eeeb59ef26dcd5"), "date" : ISODate("2018-04-11T00:00:00Z"), "month" : 4, "instrument" : "AV500", "day" : 11, "data" : "Zizic #A (MP)", "year" : 2018 }
{ "_id" : ObjectId("5bf2964823eeeb59ef26dccd"), "date" : ISODate("2018-04-12T00:00:00Z"), "month" : 4, "instrument" : "AV500", "day" : 12, "data" : "Zizic #A (MP)", "year" : 2018 }

If I use text search I don't obtain results:

db.planning.find({"year":2018, $text:{$search: "#A"}})

Is there a reason? # is a special character in mongo?

Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
Enrico Morelli
  • 185
  • 3
  • 16
  • In the documentation I read: Use the $text query operator to perform text searches on a collection with a text index. Indeed it works if I search for a word. – Enrico Morelli Jan 02 '19 at 16:11
  • Full text indexes store stemmed words and ignore all special characters in them. So MongoDB treats `"#A"` as a single-letter word `"A"`. I guess it just drops all single-letter words to optimize performance. Substituting `"#A"` for something like `"#BB"` should work fine. As for special characters, see [this question](https://stackoverflow.com/questions/28380179/stop-mongodb-from-ignoring-special-characters). – Leonid Beschastny Jan 02 '19 at 16:15
  • Thanks, its true. I've special characters like #A and I've to search for them. So I'll use regex for these. – Enrico Morelli Jan 02 '19 at 16:25

0 Answers0