Home > Software engineering >  How to avoid negation while using hyphen?
How to avoid negation while using hyphen?

Time:03-03

I have a query that looks similar to:

db.mycollection.find({
     $text: {
        $search: "my -city"
     }
})

If I run this query, it will negate city. But I want to search for the exact phrase my -city. How can I do this?

The text to search could be my-city or my - city or my -city. Is there a way I can instruct mongodb to not use negation as the default behavior and just use the exact string matching?

CodePudding user response:

Try wrapping it in "..." like this

db.mycollection.find({
     $text: {
        $search: "\"my -city\""
     }
})
  • Related