public Tuple<List<BsonDocument>, int, Stack<string>> Execute(List<string> jsonDocument,
                                          int maxInPageCount, int pageNumber,
                                         int resultCount)
        {

            var log = new Stack<string>();
            var list = new List<BsonDocument>();

            foreach (var document in jsonDocument)
            {
              
                var documentBson = BsonDocument.Parse(document);
                list.Add(documentBson);
            } 

            if (pageNumber == 0 && pageNumber != -1)
            {

                var tmp =  BuildCountQuery(list);
                resultCount = tmp.Item1;
                log.Push(tmp.Item2);
            }

            if (maxInPageCount != -1)
            {
                // Tacke the currentPage
                long skip = maxInPageCount * pageNumber;

                list = AddSkipAndTackeStages(list, skip, maxInPageCount);
            }

           var option = new AggregateOptions() { AllowDiskUse = true };

            var statTime = DateTime.Now.Ticks;

            var result =  _collection.Aggregate<BsonDocument>(list, option).ToList();


            var endDate = DateTime.Now.Ticks;
            var duration = endDate - statTime;
            var resultDuration = TimeSpan.FromTicks(duration).TotalSeconds;

            log.Push(string.Format("{0} - {1} : Duration (s) {2} ",  TimeSpan.FromTicks( statTime).ToString(), BuildStringQuery(list.Select(p => p.ToString()).ToArray()), resultDuration));

           return new Tuple<List<BsonDocument>, int, Stack<string>>(result, resultCount,log);

        }
 public MongoDbAggregateOperationViewModel(TabViewModel owner) : base(owner)
 {
     Name = Constants.AggregateOperation;
     DisplayName = "Aggregate";
     AggregateOptions = new AggregateOptions();
     ExecuteAggregate = new RelayCommand(InnerExecuteAggregate);
 }
        // private methods
        private AggregateOperation <TResult> CreateAggregateOperation <TResult>(RenderedPipelineDefinition <TResult> renderedPipeline, AggregateOptions options)
        {
            var messageEncoderSettings = GetMessageEncoderSettings();

            return(new AggregateOperation <TResult>(
                       _databaseNamespace,
                       renderedPipeline.Documents,
                       renderedPipeline.OutputSerializer,
                       messageEncoderSettings)
            {
                AllowDiskUse = options.AllowDiskUse,
                BatchSize = options.BatchSize,
                Collation = options.Collation,
                Comment = options.Comment,
                Hint = options.Hint,
                MaxAwaitTime = options.MaxAwaitTime,
                MaxTime = options.MaxTime,
                ReadConcern = _settings.ReadConcern,
                RetryRequested = _client.Settings.RetryReads,
                UseCursor = options.UseCursor
            });
        }
        public async Task<List<BsonDocument>> AggregateAsync(string databaseName, string collectionName, BsonArray pipeline, AggregateOptions options, bool explain, CancellationToken token)
        {
            return await Task.Run(() =>
            {
                var server = client.GetServer();
                var database = server.GetDatabase(databaseName);
                var collection = database.GetCollection(collectionName);
                List<BsonDocument> result;
                if (!explain)
                    result = collection.Aggregate(new AggregateArgs()
                    {
                        AllowDiskUse = options.AllowDiskUse,
                        BatchSize = options.BatchSize,
                        MaxTime = options.MaxTime,
                        Pipeline = pipeline.Select(s => s.AsBsonDocument)
                    }).ToList();
                else
                {
                    var explainResult = collection.AggregateExplain(new AggregateArgs()
                    {
                        AllowDiskUse = options.AllowDiskUse,
                        BatchSize = options.BatchSize,
                        MaxTime = options.MaxTime,
                        Pipeline = pipeline.Select(s => s.AsBsonDocument)
                    });

                    result = new List<BsonDocument>();
                    result.Add(explainResult.Response);
                }
                return result;
            }, token);
        }
Beispiel #5
0
 // constructors
 public AggregateFluent(IClientSessionHandle session, IMongoCollection <TDocument> collection, PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options)
 {
     _session    = session; // can be null
     _collection = Ensure.IsNotNull(collection, nameof(collection));
     _pipeline   = Ensure.IsNotNull(pipeline, nameof(pipeline));
     _options    = Ensure.IsNotNull(options, nameof(options));
 }
Beispiel #6
0
        public override Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(IClientSessionHandle session, PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var filteredPipeline = CreateFilteredPipeline(pipeline);

            return(_wrappedCollection.AggregateAsync(session, filteredPipeline, options, cancellationToken));
        }
 /// <inheritdoc />
 public virtual Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(IClientSessionHandle session, PipelineDefinition <NoPipelineInput, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     throw new NotImplementedException();
 }
 // public methods
 public override IAsyncCursor <TResult> Aggregate <TResult>(PipelineDefinition <NoPipelineInput, TResult> pipeline, AggregateOptions options, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(UsingImplicitSession(session => Aggregate(session, pipeline, options, cancellationToken), cancellationToken));
 }
        public override void AggregateToCollection <TResult>(IClientSessionHandle session, PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var filteredPipeline = CreateFilteredPipeline(pipeline);

            _wrappedCollection.AggregateToCollection(session, filteredPipeline, options, cancellationToken);
        }
 // constructors
 public AggregateFluent(IMongoCollection <TDocument> collection, IEnumerable <object> pipeline, AggregateOptions options, IBsonSerializer <TResult> resultSerializer)
 {
     _collection       = Ensure.IsNotNull(collection, "collection");
     _pipeline         = Ensure.IsNotNull(pipeline, "pipeline").ToList();
     _options          = Ensure.IsNotNull(options, "options");
     _resultSerializer = resultSerializer;
 }
 // constructors
 protected AggregateFluent(IClientSessionHandle session, PipelineDefinition <TInput, TResult> pipeline, AggregateOptions options)
 {
     _session  = session; // can be null
     _pipeline = Ensure.IsNotNull(pipeline, nameof(pipeline));
     _options  = Ensure.IsNotNull(options, nameof(options));
 }
Beispiel #12
0
        // public methods
        public override IAsyncCursor <TResult> Aggregate <TResult>(PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options, CancellationToken cancellationToken)
        {
            var renderedPipeline = Ensure.IsNotNull(pipeline, nameof(pipeline)).Render(_documentSerializer, _settings.SerializerRegistry);

            options = options ?? new AggregateOptions();

            var last = renderedPipeline.Documents.LastOrDefault();

            if (last != null && last.GetElement(0).Name == "$out")
            {
                var aggregateOperation = CreateAggregateToCollectionOperation(renderedPipeline, options);
                ExecuteWriteOperation(aggregateOperation, cancellationToken);

                // we want to delay execution of the find because the user may
                // not want to iterate the results at all...
                var findOperation  = CreateAggregateToCollectionFindOperation(last, renderedPipeline.OutputSerializer, options);
                var deferredCursor = new DeferredAsyncCursor <TResult>(
                    ct => ExecuteReadOperation(findOperation, ReadPreference.Primary, ct),
                    ct => ExecuteReadOperationAsync(findOperation, ReadPreference.Primary, ct));
                return(deferredCursor);
            }
            else
            {
                var aggregateOperation = CreateAggregateOperation(renderedPipeline, options);
                return(ExecuteReadOperation(aggregateOperation, cancellationToken));
            }
        }
Beispiel #13
0
 // constructors
 public AggregateFluent(IMongoCollection <TDocument> collection, IEnumerable <IPipelineStageDefinition> stages, AggregateOptions options)
 {
     _collection = Ensure.IsNotNull(collection, nameof(collection));
     _stages     = Ensure.IsNotNull(stages, nameof(stages)).ToList();
     _options    = Ensure.IsNotNull(options, nameof(options));
 }
Beispiel #14
0
 private AggregateToCollectionOperation CreateAggregateToCollectionOperation <TResult>(RenderedPipelineDefinition <TResult> renderedPipeline, AggregateOptions options)
 {
     return(new AggregateToCollectionOperation(
                _collectionNamespace,
                renderedPipeline.Documents,
                _messageEncoderSettings)
     {
         AllowDiskUse = options.AllowDiskUse,
         BypassDocumentValidation = options.BypassDocumentValidation,
         Collation = options.Collation,
         MaxTime = options.MaxTime,
         WriteConcern = _settings.WriteConcern
     });
 }
Beispiel #15
0
        private FindOperation <TResult> CreateAggregateToCollectionFindOperation <TResult>(BsonDocument outStage, IBsonSerializer <TResult> resultSerializer, AggregateOptions options)
        {
            var outputCollectionName = outStage.GetElement(0).Value.AsString;

            return(new FindOperation <TResult>(
                       new CollectionNamespace(_collectionNamespace.DatabaseNamespace, outputCollectionName),
                       resultSerializer,
                       _messageEncoderSettings)
            {
                BatchSize = options.BatchSize,
                Collation = options.Collation,
                MaxTime = options.MaxTime,
                ReadConcern = _settings.ReadConcern
            });
        }
Beispiel #16
0
 private AggregateOperation <TResult> CreateAggregateOperation <TResult>(RenderedPipelineDefinition <TResult> renderedPipeline, AggregateOptions options)
 {
     return(new AggregateOperation <TResult>(
                _collectionNamespace,
                renderedPipeline.Documents,
                renderedPipeline.OutputSerializer,
                _messageEncoderSettings)
     {
         AllowDiskUse = options.AllowDiskUse,
         BatchSize = options.BatchSize,
         Collation = options.Collation,
         MaxTime = options.MaxTime,
         ReadConcern = _settings.ReadConcern,
         UseCursor = options.UseCursor
     });
 }
        private FindOperation <TResult> CreateAggregateToCollectionFindOperation <TResult>(BsonDocument outStage, IBsonSerializer <TResult> resultSerializer, AggregateOptions options)
        {
            CollectionNamespace outputCollectionNamespace;
            var stageName = outStage.GetElement(0).Name;

            switch (stageName)
            {
            case "$out":
            {
                var outputCollectionName = outStage[0].AsString;
                outputCollectionNamespace = new CollectionNamespace(_databaseNamespace, outputCollectionName);
            }
            break;

            case "$merge":
            {
                var mergeArguments = outStage[0].AsBsonDocument;
                DatabaseNamespace outputDatabaseNamespace;
                string            outputCollectionName;
                var into = mergeArguments["into"];
                if (into.IsString)
                {
                    outputDatabaseNamespace = _databaseNamespace;
                    outputCollectionName    = into.AsString;
                }
                else
                {
                    outputDatabaseNamespace = new DatabaseNamespace(into["db"].AsString);
                    outputCollectionName    = into["coll"].AsString;
                }
                outputCollectionNamespace = new CollectionNamespace(outputDatabaseNamespace, outputCollectionName);
            }
            break;

            default:
                throw new ArgumentException($"Unexpected stage name: {stageName}.");
            }

            // because auto encryption is not supported for non-collection commands.
            // So, an error will be thrown in the previous CreateAggregateToCollectionOperation step.
            // However, since we've added encryption configuration for CreateAggregateToCollectionOperation operation,
            // it's not superfluous to also add it here
            var messageEncoderSettings = GetMessageEncoderSettings();

            return(new FindOperation <TResult>(outputCollectionNamespace, resultSerializer, messageEncoderSettings)
            {
                BatchSize = options.BatchSize,
                Collation = options.Collation,
                MaxTime = options.MaxTime,
                ReadConcern = _settings.ReadConcern,
                RetryRequested = _client.Settings.RetryReads
            });
        }
Beispiel #18
0
 /// <inheritdoc />
 public virtual IAsyncCursor <TResult> Aggregate <TResult>(PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     throw new NotImplementedException();
 }
        private AggregateToCollectionOperation CreateAggregateToCollectionOperation <TResult>(RenderedPipelineDefinition <TResult> renderedPipeline, AggregateOptions options)
        {
            var messageEncoderSettings = GetMessageEncoderSettings();

            return(new AggregateToCollectionOperation(
                       _databaseNamespace,
                       renderedPipeline.Documents,
                       messageEncoderSettings)
            {
                AllowDiskUse = options.AllowDiskUse,
                BypassDocumentValidation = options.BypassDocumentValidation,
                Collation = options.Collation,
                Comment = options.Comment,
                Hint = options.Hint,
                MaxTime = options.MaxTime,
                ReadConcern = _settings.ReadConcern,
                WriteConcern = _settings.WriteConcern
            });
        }
Beispiel #20
0
 /// <inheritdoc />
 public abstract Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
        public override IAsyncCursor <TResult> Aggregate <TResult>(IClientSessionHandle session, PipelineDefinition <NoPipelineInput, TResult> pipeline, AggregateOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(session, nameof(session));
            var renderedPipeline = Ensure.IsNotNull(pipeline, nameof(pipeline)).Render(NoPipelineInputSerializer.Instance, _settings.SerializerRegistry);

            options = options ?? new AggregateOptions();

            var lastStage     = renderedPipeline.Documents.LastOrDefault();
            var lastStageName = lastStage?.GetElement(0).Name;

            if (lastStage != null && (lastStageName == "$out" || lastStageName == "$merge"))
            {
                var aggregateOperation = CreateAggregateToCollectionOperation(renderedPipeline, options);
                ExecuteWriteOperation(session, aggregateOperation, cancellationToken);

                // we want to delay execution of the find because the user may
                // not want to iterate the results at all...
                var findOperation  = CreateAggregateToCollectionFindOperation(lastStage, renderedPipeline.OutputSerializer, options);
                var forkedSession  = session.Fork();
                var deferredCursor = new DeferredAsyncCursor <TResult>(
                    () => forkedSession.Dispose(),
                    ct => ExecuteReadOperation(forkedSession, findOperation, ReadPreference.Primary, ct),
                    ct => ExecuteReadOperationAsync(forkedSession, findOperation, ReadPreference.Primary, ct));
                return(deferredCursor);
            }
            else
            {
                var aggregateOperation = CreateAggregateOperation(renderedPipeline, options);
                return(ExecuteReadOperation(session, aggregateOperation, cancellationToken));
            }
        }
Beispiel #22
0
        // methods
        public override async Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options, CancellationToken cancellationToken)
        {
            var renderedPipeline = Ensure.IsNotNull(pipeline, nameof(pipeline)).Render(_documentSerializer, _settings.SerializerRegistry);

            options = options ?? new AggregateOptions();

            var last = renderedPipeline.Documents.LastOrDefault();

            if (last != null && last.GetElement(0).Name == "$out")
            {
                var operation = new AggregateToCollectionOperation(
                    _collectionNamespace,
                    renderedPipeline.Documents,
                    _messageEncoderSettings)
                {
                    AllowDiskUse = options.AllowDiskUse,
                    MaxTime      = options.MaxTime
                };

                await ExecuteWriteOperationAsync(operation, cancellationToken).ConfigureAwait(false);

                var outputCollectionName = last.GetElement(0).Value.AsString;

                var findOperation = new FindOperation <TResult>(
                    new CollectionNamespace(_collectionNamespace.DatabaseNamespace, outputCollectionName),
                    renderedPipeline.OutputSerializer,
                    _messageEncoderSettings)
                {
                    BatchSize = options.BatchSize,
                    MaxTime   = options.MaxTime
                };

                // we want to delay execution of the find because the user may
                // not want to iterate the results at all...
                return(await Task.FromResult <IAsyncCursor <TResult> >(new DeferredAsyncCursor <TResult>(ct => ExecuteReadOperationAsync(findOperation, ReadPreference.Primary, ct))).ConfigureAwait(false));
            }
            else
            {
                var aggregateOperation = new AggregateOperation <TResult>(
                    _collectionNamespace,
                    renderedPipeline.Documents,
                    renderedPipeline.OutputSerializer,
                    _messageEncoderSettings)
                {
                    AllowDiskUse = options.AllowDiskUse,
                    BatchSize    = options.BatchSize,
                    MaxTime      = options.MaxTime,
                    UseCursor    = options.UseCursor
                };
                return(await ExecuteReadOperationAsync(aggregateOperation, cancellationToken).ConfigureAwait(false));
            }
        }
Beispiel #23
0
        // public methods
        public override IAsyncCursor <TResult> Aggregate <TResult>(PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var filteredPipeline = CreateFilteredPipeline(pipeline);

            return(_wrappedCollection.Aggregate(filteredPipeline, options, cancellationToken));
        }
Beispiel #24
0
 /// <summary>
 /// Begins a fluent aggregation interface.
 /// </summary>
 /// <typeparam name="TDocument">The type of the document.</typeparam>
 /// <param name="collection">The collection.</param>
 /// <param name="options">The options.</param>
 /// <returns>
 /// A fluent aggregate interface.
 /// </returns>
 public static IAggregateFluent <TDocument> Aggregate <TDocument>(this IMongoCollection <TDocument> collection, AggregateOptions options = null)
 {
     return(new AggregateFluent <TDocument, TDocument>(collection, Enumerable.Empty <IPipelineStageDefinition>(), options ?? new AggregateOptions()));
 }
Beispiel #25
0
        /// <summary>
        /// Begins a fluent aggregation interface.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <param name="collection">The collection.</param>
        /// <param name="options">The options.</param>
        /// <returns>
        /// A fluent aggregate interface.
        /// </returns>
        public static IAggregateFluent <TDocument, TDocument> Aggregate <TDocument>(this IMongoCollection <TDocument> collection, AggregateOptions options = null)
        {
            AggregateOptions <TDocument> newOptions;

            if (options == null)
            {
                newOptions = new AggregateOptions <TDocument>
                {
                    ResultSerializer = collection.DocumentSerializer
                };
            }
            else
            {
                newOptions = new AggregateOptions <TDocument>
                {
                    AllowDiskUse     = options.AllowDiskUse,
                    BatchSize        = options.BatchSize,
                    MaxTime          = options.MaxTime,
                    ResultSerializer = collection.DocumentSerializer,
                    UseCursor        = options.UseCursor
                };
            }
            return(new AggregateFluent <TDocument, TDocument>(collection, new List <object>(), newOptions));
        }
Beispiel #26
0
 // methods
 public IAggregateFluent <TDocument, TDocument> Aggregate(AggregateOptions options)
 {
     options = options ?? new AggregateOptions();
     return(new AggregateFluent <TDocument, TDocument>(this, new List <object>(), options, _serializer));
 }
 /// <inheritdoc />
 public virtual void AggregateToCollection <TResult>(PipelineDefinition <NoPipelineInput, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Begins a fluent aggregation interface.
        /// </summary>
        /// <param name="database">The database.</param>
        /// <param name="session">The session.</param>
        /// <param name="options">The options.</param>
        /// <returns>
        /// A fluent aggregate interface.
        /// </returns>
        public static IAggregateFluent <NoPipelineInput> Aggregate(this IMongoDatabase database, IClientSessionHandle session, AggregateOptions options = null)
        {
            Ensure.IsNotNull(session, nameof(session));
            var emptyPipeline = new EmptyPipelineDefinition <NoPipelineInput>(NoPipelineInputSerializer.Instance);

            return(new DatabaseAggregateFluent <NoPipelineInput>(session, database, emptyPipeline, options ?? new AggregateOptions()));
        }
        /// <summary>
        /// Begins a fluent aggregation interface.
        /// </summary>
        /// <param name="database">The database.</param>
        /// <param name="options">The options.</param>
        /// <returns>
        /// A fluent aggregate interface.
        /// </returns>
        public static IAggregateFluent <NoPipelineInput> Aggregate(this IMongoDatabase database, AggregateOptions options = null)
        {
            var emptyPipeline = new EmptyPipelineDefinition <NoPipelineInput>(NoPipelineInputSerializer.Instance);

            return(new DatabaseAggregateFluent <NoPipelineInput>(null, database, emptyPipeline, options ?? new AggregateOptions()));
        }
Beispiel #30
0
        public async Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(IEnumerable <object> pipeline, AggregateOptions <TResult> options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(pipeline, "pipeline");

            options = options ?? new AggregateOptions <TResult>();
            var pipelineDocuments = pipeline.Select(x => ConvertToBsonDocument(x)).ToList();

            var last = pipelineDocuments.LastOrDefault();

            if (last != null && last.GetElement(0).Name == "$out")
            {
                var operation = new AggregateToCollectionOperation(
                    _collectionNamespace,
                    pipelineDocuments,
                    _messageEncoderSettings)
                {
                    AllowDiskUse = options.AllowDiskUse,
                    MaxTime      = options.MaxTime
                };

                await ExecuteWriteOperation(operation, cancellationToken).ConfigureAwait(false);

                var outputCollectionName = last.GetElement(0).Value.AsString;
                var resultSerializer     = ResolveResultSerializer(options.ResultSerializer);

                var findOperation = new FindOperation <TResult>(
                    new CollectionNamespace(_collectionNamespace.DatabaseNamespace, outputCollectionName),
                    resultSerializer,
                    _messageEncoderSettings)
                {
                    BatchSize = options.BatchSize,
                    MaxTime   = options.MaxTime
                };

                // we want to delay execution of the find because the user may
                // not want to iterate the results at all...
                return(await Task.FromResult <IAsyncCursor <TResult> >(new DeferredAsyncCursor <TResult>(ct => ExecuteReadOperation(findOperation, ct))).ConfigureAwait(false));
            }
            else
            {
                var operation = CreateAggregateOperation <TResult>(options, pipelineDocuments);
                return(await ExecuteReadOperation(operation, cancellationToken).ConfigureAwait(false));
            }
        }