How to batch retrieve documents with mongoDB?
Posted
by
edude05
on Stack Overflow
See other posts from Stack Overflow
or by edude05
Published on 2011-01-11T18:48:47Z
Indexed on
2011/01/13
21:53 UTC
Read the original article
Hit count: 209
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
© Stack Overflow or respective owner