1

The goal of this code is to display the current numbers of death, recoveries and critical for covid 19 around the world.

The search function codes are as follows:

const search = (e) => {
        e.preventDefault() //to avoid page redirection
        const countryMatch = countryCollection.find(country => country.country_name === targetCountry)
        if (!countryMatch || countryMatch === null|| countryMatch === 'undefined') {
            alert("Country Does Not Exist, use another name.")
            setName("")
            setTargetCountry("")
        } else {
            setName(countryMatch.country_name)
            
            setDeathCount(toNum(countryMatch.deaths))
            setCriticalCount(toNum(countryMatch.serious_critical))
            setRecoveryCount(toNum(countryMatch.total_recovered))
        }
    }

Our task is to find a country regardless if its in upper or lower case. Eg: Malaysia vs malaysia.

  • Does this answer your question? [How to do case insensitive string comparison?](https://stackoverflow.com/questions/2140627/how-to-do-case-insensitive-string-comparison) – He3lixxx Jun 22 '21 at 17:43

2 Answers2

0

REGULAR EXPRESSION

What you need is regular expression or RegExp. MongoDb supports regular expression for your searches.

In Your case it can be something like

countryCollections.find({'country':new RegExp(countryName,flag)},callback)

flag determines how you want to search for case insensitive search use 'i'

More about RegExp can be found on mongoDB docs https://docs.mongodb.com/manual/reference/operator/query/regex/

phantom
  • 18
  • 7
0

According to your usage of MongoDB, I would say, that this case is an excellent case to using text indexes.

Here is an example for you:

Schema.index( 
  // making field available for $text search and $meta sorting
  {
    'field': 'text', 
    'embedDoc.field': 'text',
  },
  {
    //options of index
    weights: // weight for each field
      {
        'field': 2,
        'embedDoc.field': 1,
      },
    name: 'Countries', // Index Name for Mongo Compass and .explain debug
  })

I guess you should try that. It will solve all your potential problems with text search. Like ' or diacritic symbols in searching, lower-uppercase and so on. But please, check the documentation of text indexes, before implementing them, it's quite sensitive and flexible for any cases. But there is no universal silver bullet.

AlexZeDim
  • 3,520
  • 2
  • 28
  • 64