예제 #1
0
        public IEnumerator <BsonDocument> GetEnumerator()
        {
            AggregateResult result;

            if (_immediateExecutionResult != null)
            {
                result = _immediateExecutionResult;
                _immediateExecutionResult = null;
            }
            else
            {
                result = _collection.RunAggregateCommand(_operations, _options);
            }

            if (result.CursorId != 0)
            {
                var connectionProvider = new ServerInstanceConnectionProvider(result.ServerInstance);
                var readerSettings     = new BsonBinaryReaderSettings
                {
                    Encoding           = _collection.Settings.ReadEncoding ?? MongoDefaults.ReadEncoding,
                    GuidRepresentation = _collection.Settings.GuidRepresentation
                };
                return(new CursorEnumerator <BsonDocument>(
                           connectionProvider,
                           _collection.FullName,
                           result.ResultDocuments,
                           result.CursorId,
                           _options.BatchSize ?? 0,
                           0,
                           readerSettings,
                           BsonDocumentSerializer.Instance,
                           null));
            }
            else if (result.OutputNamespace != null)
            {
                var ns                 = result.OutputNamespace;
                var firstDot           = ns.IndexOf('.');
                var databaseName       = ns.Substring(0, firstDot);
                var collectionName     = ns.Substring(firstDot + 1);
                var database           = _collection.Database.Server.GetDatabase(databaseName);
                var collectionSettings = new MongoCollectionSettings {
                    ReadPreference = ReadPreference.Primary
                };
                var collection = database.GetCollection <BsonDocument>(collectionName, collectionSettings);
                return(collection.FindAll().GetEnumerator());
            }
            else if (result.ResultDocuments != null)
            {
                return(result.ResultDocuments.GetEnumerator());
            }
            else
            {
                throw new NotSupportedException("Unexpected response to aggregate command.");
            }
        }
        // public methods
        public IEnumerator <BsonDocument> GetEnumerator()
        {
            if (_outputCollectionName != null)
            {
                var database           = _collection.Database;
                var collectionSettings = new MongoCollectionSettings {
                    ReadPreference = ReadPreference.Primary
                };
                var collection = database.GetCollection <BsonDocument>(_outputCollectionName, collectionSettings);
                return(collection.FindAll().GetEnumerator());
            }

            var result = _collection.RunAggregateCommand(_args);

            if (result.CursorId != 0)
            {
                var connectionProvider = new ServerInstanceConnectionProvider(result.ServerInstance);
                var readerSettings     = new BsonBinaryReaderSettings
                {
                    Encoding           = _collection.Settings.ReadEncoding ?? MongoDefaults.ReadEncoding,
                    GuidRepresentation = _collection.Settings.GuidRepresentation
                };
                return(new CursorEnumerator <BsonDocument>(
                           connectionProvider,
                           _collection.FullName,
                           result.ResultDocuments,
                           result.CursorId,
                           _args.BatchSize ?? 0,
                           0,
                           readerSettings,
                           BsonDocumentSerializer.Instance,
                           null));
            }
            else if (result.ResultDocuments != null)
            {
                return(result.ResultDocuments.GetEnumerator());
            }
            else
            {
                throw new NotSupportedException("Unexpected response to aggregate command.");
            }
        }