I have a code that searches people in MongoDB by name. The problem with my code is that its not scaling up, I tried to run this code with 40 threads but when I wacthed the reaction in my DB, I had to close it because it was taking 100% of the CPU. All three fields being searched are indexed in MongoDB. Is there a way to improve the query? I need to run 5 million names and my DB has over 200 million people.
public Person searchPerson(string name)
{
string MongoUser = "MONGO_USERNAME";
string MongoPass = "MONGO_PASSWORD";
string MongoURL = "MONGO_URL";
string MongoDB = "MONGO_DB";
MongoClient _client = new MongoClient("mongodb://" + MongoUser + ":" + MongoPass + "@" + MongoURL);
IMongoCollection<BazingaPerson> myCollection = _client.GetDatabase (MongoDB).GetCollection<Person> ("COLLECTION_NAME");
List<Person> peopleList = myCollection.AsQueryable<Person>().Where(e => e.Name == name).ToList<Person>();
// both functions below only transform string like replace, substring or splits . They dont query in a DB or make web requests
string nameInitials = getInitials(name);
string phoneticName = getPhoneticName(name);
if(peopleList.Count() == 0) peopleList = myCollection.AsQueryable<Person>().Where(e => e.StandardName.Equals (phoneticName)).ToList<Person>();
if(peopleList.Count() == 0) peopleList = myCollection.AsQueryable<Person>().Where(e => e.Initials.Equals (nameInitials)).ToList<Person>();
if(peopleList.Count() == 0) return null;
return peopleList[0];
}