Retrieving entire data collection from a RavenDB

前端 未结 6 934
囚心锁ツ
囚心锁ツ 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:24

    I like Al Dass solution of getting ids to operate on instead of complete large objects. Also getting the ids directly from the index. However the recursion scares me a bit (even though I think it might be ok) and I removed the reflection.

    public List GetAllIds()
    {
    var allIds = new List();
    IDocumentSession session = null;
    
    try
    {
        session = documentStore.OpenSession();
        int queryCount = 0;
        int start = 0;
        while (true)
        {
            var current = session.Advanced.DocumentQuery()
                .Take(1024)
                .Skip(start)
                .SelectFields("__document_id")
                .AddOrder("__document_id")
                .ToList();
    
            if (current.Count == 0)
                break;
            allIds.AddRange(current);
    
            queryCount += 1;
            start += current.Count;
    
            if (queryCount == 30)
            {
                queryCount = 0;
                session.Dispose();
                session = documentStore.OpenSession();
            }
        }
    }
    finally
    {
        if (session != null)
        {
            session.Dispose();
        }
    }
    
    return allIds;
    }
    

    also, this is updated to ravendb 3

提交回复
热议问题