I am using MongoDB and have a pretty small collection of documents, here it is:
name: 'Always en_GB locale, default name',
ticker: 'Derivative short name from the name field. Like if my full name is Aleksandr, then ticker will be Sasha'
name_locale: 'This field has a string in different locales, sometimes it\'s Cyrillic, sometimes it\'s Spanish',
locale: 'en_GB', //or 'en_US', it shows the locale of name_locale field
region 'Europe', //or North America, it doesn't matter in that case
All these fields are String
type only.
Also, I have an API endpoint, and I want to receive a parameter, that could be any value of these fields. So it could be param=Europe
or param=en_GB
or param=name_locale_value
.
So, in that case, I need a text index, right? And I have it.
schema.index(
{ name: 'text', name_locale: 'text', ticker: 'text', region: 'text' },
{ name: 'SearchQuery' },
);
but as you may notice, I don't have any collation and strength values. So the question is:
How to make case-insensitive searches, with
strength: 1
, like in this answer, if I also need to include/search byname_locale
field, that has string values in different languages?
Docs example:
{
name: 'Aleksandr,
ticker 'Sasha',
name_locale: 'Саша',
locale: 'ru_RU',
region: 'Europe',
},{
name: 'Jonathan',
ticker 'John',
name_locale: 'Jonathan',
locale: 'en_US',
region: 'North America',
}
await collection.find(
{ $text: { $search: QueryValue } }, //QueryValue = 'europe'
{ score: { $meta: 'textScore' } },
)
- Do I need to build many single field indexes? And use
$or
operator to find through all the fields. - Or does
text
indexes in Mongo 4.4 supportsstrength
withoutlocale
? - Maybe there is another universal way, like using
.find
with Regexp? I will be grateful if you share some of your advice.