public override string ToString()
        {
            var sb = new StringBuilder("aggregate([");

            if (_stages.Count > 0)
            {
                var pipeline         = new PipelineStagePipelineDefinition <TDocument, TResult>(_stages);
                var renderedPipeline = pipeline.Render(_collection.DocumentSerializer, _collection.Settings.SerializerRegistry);
                sb.Append(string.Join(", ", renderedPipeline.Documents.Select(x => x.ToString())));
            }
            sb.Append("])");
            return(sb.ToString());
        }
        public override IOrderedAggregateFluent <TResult> ThenBy(SortDefinition <TResult> newSort)
        {
            Ensure.IsNotNull(newSort, nameof(newSort));
            var stages       = _pipeline.Stages.ToList();
            var oldSortStage = (SortPipelineStageDefinition <TResult>)stages[stages.Count - 1];
            var oldSort      = oldSortStage.Sort;
            var combinedSort = Builders <TResult> .Sort.Combine(oldSort, newSort);

            var combinedSortStage = PipelineStageDefinitionBuilder.Sort(combinedSort);

            stages[stages.Count - 1] = combinedSortStage;
            var newPipeline = new PipelineStagePipelineDefinition <TDocument, TResult>(stages);

            return((IOrderedAggregateFluent <TResult>)WithPipeline(newPipeline));
        }
        private PipelineDefinition <TDocument, TResult> CreateFilteredPipeline <TResult>(PipelineDefinition <TDocument, TResult> pipeline)
        {
            const string matchOperatorName = "$match";

            var filterStage = new DelegatedPipelineStageDefinition <TDocument, TDocument>(
                matchOperatorName,
                (s, sr) =>
            {
                var renderedFilter = _filter.Render(s, sr);
                return(new RenderedPipelineStageDefinition <TDocument>(matchOperatorName, new BsonDocument(matchOperatorName, renderedFilter), s));
            });

            var filterPipeline   = new PipelineStagePipelineDefinition <TDocument, TDocument>(new[] { filterStage });
            var combinedPipeline = new CombinedPipelineDefinition <TDocument, TDocument, TResult>(
                filterPipeline,
                pipeline);

            return(new OptimizingPipelineDefinition <TDocument, TResult>(combinedPipeline));
        }
Example #4
0
        // public methods
        public override Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            const string matchOperatorName = "$match";

            var filterStage = new DelegatedPipelineStageDefinition <TDocument, TDocument>(
                matchOperatorName,
                (s, sr) =>
            {
                var renderedFilter = _filter.Render(s, sr);
                return(new RenderedPipelineStageDefinition <TDocument>(matchOperatorName, new BsonDocument(matchOperatorName, renderedFilter), s));
            });

            var filterPipeline   = new PipelineStagePipelineDefinition <TDocument, TDocument>(new[] { filterStage });
            var combinedPipeline = new CombinedPipelineDefinition <TDocument, TDocument, TResult>(
                filterPipeline,
                pipeline);

            var optimizedPipeline = new OptimizingPipelineDefinition <TDocument, TResult>(combinedPipeline);

            return(_wrappedCollection.AggregateAsync(optimizedPipeline, options, cancellationToken));
        }
        public override Task <IAsyncCursor <TResult> > ToCursorAsync(CancellationToken cancellationToken)
        {
            var pipeline = new PipelineStagePipelineDefinition <TDocument, TResult>(_stages);

            return(_collection.AggregateAsync(pipeline, _options, cancellationToken));
        }