Retrieving entire data collection from a RavenDB

前端 未结 6 952
囚心锁ツ
囚心锁ツ 2021-02-01 21:55

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 cl

6条回答
  •  时光取名叫无心
    2021-02-01 22:40

    Building up on the Ayende answer, here is a complete method, that does overcome the problem of 30 queries per session and indeed return all documents of the supplied class:

        public static List getAll(DocumentStore docDB) {
            return getAllFrom(0, new List(), docDB);
        }
    
        public static List getAllFrom(int startFrom, List list, DocumentStore docDB ) {
            var allUsers = list;
    
            using (var session = docDB.OpenSession())
            {
                int queryCount = 0;
                int start = startFrom;
                while (true)
                {
                    var current = session.Query().Take(1024).Skip(start).ToList();
                    queryCount += 1;
                    if (current.Count == 0)
                        break;
    
                    start += current.Count;
                    allUsers.AddRange(current);
    
                    if (queryCount >= 30)
                    {
                        return getAllFrom(start, allUsers, docDB);
                    }
                }
            }
            return allUsers;
        }
    

    I hope it is not too hacky to do it like this.

提交回复
热议问题