Retrieving entire data collection from a RavenDB

annantDev picture annantDev · Jun 29, 2012 · Viewed 9k times · Source

I have a requirement where I need to fetch the entire data collection Users from RavenDB and compare the retrieved result set with another set of data. There are close to 4000 records in this particular collection.

Because Raven is Safe By Default, I keep getting an exception for either Number of requests per session exceeded or it returns the maximum 128 records.

I don't want to set the property Session.Advanced.MaxNumberOfRequestsPerSession to a higher value.

What query should I use to get the count of all the records? What is the ideal approach to handle this situation?

Answer

Ayende Rahien picture Ayende Rahien · Jun 30, 2012

You use paging, and read this 1024 items at a time.

int start = 0;
while(true)
{
   var current = session.Query<User>().Take(1024).Skip(start).ToList();
   if(current.Count == 0)
          break;

   start+= current.Count;
   allUsers.AddRange(current);

}