Hello everyone,
I have an application that queries data from a mongoDB using the mongoDB C# driver something like this:
public void main()
{
foreach (int i in listOfKey)
{
list.add(getObjectfromDB(i);
}
}
public myObject getObjFromDb(int primaryKey)
{
document query = new document();
query["primKey"] = primaryKey;
document result= mongo["myDatabase"]["myCollection"].findOne(query);
return parseObject(result);
}
On my local (development) machine to get 100 object this way takes less than a second. However, I recently moved the database to a server on the internet, and this query takes about 30 seconds to execute for the same number of object.
Furthermore, looking at the mongoDB log, it seems to open about 8-10 connections to the DB to perform this query.
So what I'd like to do is have the query the database for an array of primaryKeys and get them all back at once, then do the parsing in a loop afterwards, using one connection if possible.
How could I optimize my query to do so?
Thanks,
--Michael