开发者

How to batch retrieve documents with mongoDB?

开发者 https://www.devze.com 2023-02-03 12:22 出处:网络
I have an application that queries data from a mongoDB using the mongoDB C# driver something like this:

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["primK开发者_StackOverflow社区ey"] = 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


You want to use $in. $or will work as well, but is more verbose and only supported in newer versions (post 1.5.4 I believe).


it sounds like what you're looking for is the magical "$in" statement in your query. i'm not familiar with C# but in json, you'd be looking at a query similar to:

query = {
   'primaryKey': {
      '$in': [
         'val1',
         'val2',
         'val3'
      ]
   }
}

The above will return a list of results as a cursor when calling .find(), opposed to what you're doing now where you're submitting a single primaryKey and calling findOne().

find() returns a cursor (opposed to findOne() which returns a data structure) so to access the data you'll have to loop through the cursor object to grab each document.

Hope this helps! Feel free to ask any more specific questions in the comments.

MongoDB $or reference: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24or

C# find() reference: http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-FindandFindAsmethods

0

精彩评论

暂无评论...
验证码 换一张
取 消