简体   繁体   English

C# MongoDB 驱动程序只返回 100 个结果

[英]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.我在 Collection 上有 829 个文档,但是当我检索它们时,我只得到 100 个文档。

I have this LINQ code:我有这个 LINQ 代码:

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.我在 Collection 上有 829 个文档,但是当我检索它们时,我只得到 100 个文档。

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.我可以在我这边重现这个问题,使用 IMongoCollection collection.AsQueryable()上的 AsQueryable 扩展方法来查找集合中的文档,即使我在 Azure 门户上将每页设置更改为无限,它也总是返回 100 个文档。

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.要查询集合中的所有文档,如您在评论中提到的,您可以尝试使用空过滤器调用Find 方法

You're probably being limited by the default cursor BatchSize .您可能受到默认光标 BatchSize 的限制。 You can modify this behaviour passing an AggregateOptions object to the AsQueryable extension and setting the BatchSize property to a large enough value.您可以修改此行为,将AggregateOptions对象传递给AsQueryable扩展并将BatchSize属性设置为足够大的值。

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

I found the question useful, so I wrote a convenient IEnumerator :我发现这个问题很有用,所以我写了一个方便的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
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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