简体   繁体   中英

C# MongoDB driver only returning 100 results

I am writing a mailing label, and need to print a label for each document.

I have 829 documents on the Collection, but when I retrieve them, I only get 100 documents.

I have this LINQ code:

IMongoCollection Pessoa;
Pessoa = database.GetCollection<Pessoa>(collectionName);

return Pessoa.AsQueryable().ToList();

How to retrieve ALL the documents?

I have 829 documents on the Collection, but when I retrieve them, I only get 100 documents.

I could reproduce the issue on my side, using AsQueryable extension method on IMongoCollection collection.AsQueryable() to find documents in a collection, which always return 100 documents even though I changed Items per page setting to Unlimited on Azure portal.

Setting:

在此处输入图片说明

Test code:

在此处输入图片说明

Count documents in query explorer:

在此处输入图片说明

To query all the documents in a collection, as you mentioned in comment, you could try to call Find method with an empty filter.

You're probably being limited by the default cursor BatchSize . You can modify this behaviour passing an AggregateOptions object to the AsQueryable extension and setting the BatchSize property to a large enough value.

public static IMongoQueryable<TDocument> AsQueryable<TDocument>(this IMongoCollection<TDocument> collection, AggregateOptions aggregateOptions = null)

I found the question useful, so I wrote a convenient IEnumerator :

        private sealed class MongoCollectionEnumerator : IEnumerator<T> {
            private IMongoCollection<T> _collection;
            private IAsyncCursor<T> _cursor; // outer enumerator
            private IEnumerator<T> _currentBatchEnumerator; // inner enumerator

            public MongoCollectionEnumerator(IMongoCollection<T> collection) {
                _collection = collection;

                InternalInit();
            }

            #region interface implementation
            T IEnumerator<T>.Current {
                get {
                    return _currentBatchEnumerator.Current;
                }
            }

            object IEnumerator.Current {
                get {
                    return ThisAsTypedIEnumerator.Current;
                }
            }

            bool IEnumerator.MoveNext() {
                if (_currentBatchEnumerator != null) {
                    if (_currentBatchEnumerator.MoveNext()) {
                        return true;
                    }
                }

                // inner not initialized or already at end
                if (_cursor.MoveNext()) {
                    // advance the outer and defer back to the inner by recursing
                    _currentBatchEnumerator = _cursor.Current.GetEnumerator();
                    return ThisAsIEnumerator.MoveNext();
                }
                else { // outer cannot advance, this is the end
                    return false;
                }
            }

            void IEnumerator.Reset() {
                InternalCleanUp();
                InternalInit();
            }
            #endregion

            #region methods private
            // helper properties to retrieve an explicit interface-casted this
            private IEnumerator ThisAsIEnumerator => this;
            private IEnumerator<T> ThisAsTypedIEnumerator => this;

            private void InternalInit() {
                var filterBuilder = new FilterDefinitionBuilder<T>();
                _cursor = _collection.Find(filterBuilder.Empty).ToCursor();
            }

            private void InternalCleanUp() {
                if (_currentBatchEnumerator != null) {
                    _currentBatchEnumerator.Reset();
                    _currentBatchEnumerator = null;
                }

                if (_cursor != null) {
                    _cursor.Dispose();
                    _cursor = null;
                }
            }
            #endregion

            #region IDisposable implementation
            private bool disposedValue = false; // To detect redundant calls

            private void InternalDispose(bool disposing) {
                if (!disposedValue) {
                    if (disposing) {
                        InternalCleanUp();

                        _collection = null;
                    }

                    disposedValue = true;
                }
            }

            void IDisposable.Dispose() {
                InternalDispose(true);
            }
            #endregion
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM