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

时间:2017-04-19 14:03:21

标签: c# mongodb linq azure-cosmosdb

我正在写邮件标签,需要为每个文档打印一个标签。

我在Collection上有829个文档,但是当我检索它们时,我只能获得100个文档。

我有这个LINQ代码:

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

return Pessoa.AsQueryable().ToList();

如何检索所有文件?

3 个答案:

答案 0 :(得分:2)

  

我在Collection上有829个文档,但是当我检索它们时,我只能获得100个文档。

我可以在IMongoCollection collection.AsQueryable()上使用AsQueryable扩展方法重现问题,在集合中查找文档,即使我更改了每页项目数设置,也始终返回100个文档Azure门户网站上的无限制。

环境:

enter image description here

测试代码:

enter image description here

在查询资源管理器中计算文档:

enter image description here

要查询集合中的所有文档,如评论中所述,您可以尝试使用空过滤器调用Find method

答案 1 :(得分:2)

您可能受到默认cursor BatchSize的限制。 您可以修改此行为,将AggregateOptions对象传递到AsQueryable扩展名,并将BatchSize属性设置为足够大的值。

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

答案 2 :(得分:0)

我发现这个问题很有用,所以写了一个方便的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
        }
相关问题