Proper Way to Retrieve More than 128 Documents with RavenDB

前端 未结 5 1868
执念已碎
执念已碎 2020-12-13 01:07

I know variants of this question have been asked before (even by me), but I still don\'t understand a thing or two about this...

It was my understanding that one cou

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 01:36

    You can also use a predefined index with the Stream method. You may use a Where clause on indexed fields.

    var query = session.Query();
    var query = session.Query().Where(x => !x.IsDeleted);
    
    using (var enumerator = session.Advanced.Stream(query))
    {
        while (enumerator.MoveNext())
        {
            var user = enumerator.Current.Document;
            // do something
        }
    }
    

    Example index:

    public class MyUserIndex: AbstractIndexCreationTask
    {
        public MyUserIndex()
        {
            this.Map = users =>
                from u in users
                select new
                {
                    u.IsDeleted,
                    u.Username,
                };
        }
    }
    

    Documentation: What are indexes? Session : Querying : How to stream query results?


    Important note: the Stream method will NOT track objects. If you change objects obtained from this method, SaveChanges() will not be aware of any change.


    Other note: you may get the following exception if you do not specify the index to use.

    InvalidOperationException: StreamQuery does not support querying dynamic indexes. It is designed to be used with large data-sets and is unlikely to return all data-set after 15 sec of indexing, like Query() does.

提交回复
热议问题