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 AggregateQueryResult( MongoCollection collection, IEnumerable <BsonDocument> operations, MongoAggregateOptions options, AggregateResult immediateExecutionResult) { _collection = collection; _operations = operations; // TODO: make a defensive copy? _options = options; // TODO: make a defensive copy? _immediateExecutionResult = immediateExecutionResult; }
private static ITrackerReportResults ToMongoAggregationResult(IReportSpecification specification, AggregateResult result) { const string utcDateKey = "UtcDateTime"; const string totalKey = "_Total"; const string occurrenceKey = "_Occurrence"; int count = result.ResultDocuments.Count(); List<string> names = result.ResultDocuments.First().Names.Where(x => !(x == utcDateKey || x == "_id")).ToList(); var results = new MongoTrackerResults(specification); foreach (BsonDocument document in result.ResultDocuments) { BsonDocument dateTime = document[utcDateKey].AsBsonDocument; long total = document[totalKey].ToInt64(); long occurrence = document[occurrenceKey].ToInt64(); DateTime utcDateTime = ConvertDateTimeDocumentToDateTime(dateTime); IAggregationResult trackerResult = results.AddAggregationResult(utcDateTime, occurrence, total); foreach (string key in names) { string fullyQualifiedName = key.GetFullyQualifiedNameFromFormattedString(); BsonValue measurementResult = document[key]; IMeasurement measurement = specification.Counters.FirstOrDefault(x => x.FullyQualifiedPropertyName == fullyQualifiedName); if (measurement != null) trackerResult.AddMeasurementResult(measurement, measurementResult.ToString()); } } return results; }
private static ITrackerReportResults ToMongoAggregationResult(IReportSpecification specification, AggregateResult queryResults) { const string utcDateKey = "UtcDateTime"; const string totalKey = "_Total"; const string occurrenceKey = "_Occurrence"; const string KEY_FILTER = "KeyFilter"; const string ID_KEY = "_id"; const string TYPE_KEY = "Type"; var results = new MongoTrackerResults(specification); if (! queryResults.ResultDocuments.Any()) return results; int count = queryResults.ResultDocuments.Count(); List<string> names = queryResults.ResultDocuments.First().Names.Where(x => !(x == utcDateKey || x == "_id")).ToList(); foreach (BsonDocument document in queryResults.ResultDocuments) { BsonDocument dateTime = document[utcDateKey].AsBsonDocument; long total = document[totalKey].ToInt64(); long occurrence = document[occurrenceKey].ToInt64(); string typeName = document[ID_KEY][TYPE_KEY].AsString; string keyFilter = document[ID_KEY][KEY_FILTER].ToString(); DateTime utcDateTime = ConvertDateTimeDocumentToDateTime(dateTime); IAggregationBuildableResult trackerResult = results.AddAggregationResult(utcDateTime, typeName, keyFilter, occurrence, total); var relevantNames = names.Where(x => x.GetFullyQualifiedNameFromFormattedString().StartsWith(typeName)); foreach (string key in relevantNames ) { string fullyQualifiedName = key.GetFullyQualifiedNameFromFormattedString(); BsonValue measurementResult = document[key]; IMeasurement measurement = specification.Counters.FirstOrDefault(x => x.FullyQualifiedPropertyName == fullyQualifiedName); if (measurement != null) trackerResult.AddMeasurementResult(measurement, measurementResult.ToString()); } } return results; }