Beispiel #1
0
        public override IAggregateFluent <AggregateBucketResult <TValue> > Bucket <TValue>(
            AggregateExpressionDefinition <TResult, TValue> groupBy,
            IEnumerable <TValue> boundaries,
            Optional <TValue> defaultBucket = default(Optional <TValue>))
        {
            const string operatorName = "$bucket";
            var          stage        = new DelegatedPipelineStageDefinition <TResult, AggregateBucketResult <TValue> >(
                operatorName,
                (s, sr) =>
            {
                var valueSerializer         = sr.GetSerializer <TValue>();
                var renderedGroupBy         = groupBy.Render(s, sr);
                var serializedBoundaries    = boundaries.Select(b => valueSerializer.ToBsonValue(b));
                var serializedDefaultBucket = defaultBucket.HasValue ? valueSerializer.ToBsonValue(defaultBucket.Value) : null;
                var document = new BsonDocument
                {
                    { operatorName, new BsonDocument
                      {
                          { "groupBy", renderedGroupBy },
                          { "boundaries", new BsonArray(serializedBoundaries) },
                          { "default", serializedDefaultBucket, serializedDefaultBucket != null }
                      } }
                };
                return(new RenderedPipelineStageDefinition <AggregateBucketResult <TValue> >(
                           operatorName,
                           document,
                           sr.GetSerializer <AggregateBucketResult <TValue> >()));
            });

            return(AppendStage(stage));
        }
Beispiel #2
0
        public override IAggregateFluent <AggregateBucketAutoResult <TValue> > BucketAuto <TValue>(
            AggregateExpressionDefinition <TResult, TValue> groupBy,
            int buckets,
            AggregateBucketAutoOptions options = null)
        {
            Ensure.IsNotNull(groupBy, nameof(groupBy));
            Ensure.IsGreaterThanZero(buckets, nameof(buckets));

            const string operatorName = "$bucketAuto";
            var          stage        = new DelegatedPipelineStageDefinition <TResult, AggregateBucketAutoResult <TValue> >(
                operatorName,
                (s, sr) =>
            {
                var renderedGroupBy = groupBy.Render(s, sr);
                var document        = new BsonDocument
                {
                    { operatorName, new BsonDocument
                      {
                          { "groupBy", renderedGroupBy },
                          { "buckets", buckets },
                          { "granularity", () => options.Granularity.Value.Value, options != null && options.Granularity.HasValue }
                      } }
                };
                return(new RenderedPipelineStageDefinition <AggregateBucketAutoResult <TValue> >(
                           operatorName,
                           document,
                           sr.GetSerializer <AggregateBucketAutoResult <TValue> >()));
            });

            return(AppendStage(stage));
        }
Beispiel #3
0
        public override IAggregateFluent <TNewResult> GraphLookup <TNewResult, TFrom, TConnect, TConnectFrom, TStartWith, TAs, TAsEnumerable>(
            IMongoCollection <TFrom> from,
            FieldDefinition <TFrom, TConnectFrom> connectFromField,
            FieldDefinition <TFrom, TConnect> connectToField,
            AggregateExpressionDefinition <TResult, TStartWith> startWith,
            FieldDefinition <TNewResult, TAsEnumerable> @as,
            FieldDefinition <TAs, int> depthField,
            AggregateGraphLookupOptions <TNewResult, TFrom, TConnect, TConnectFrom, TStartWith, TAs, TAsEnumerable> options = null)
        {
            Ensure.IsNotNull(from, nameof(from));
            Ensure.IsNotNull(connectFromField, nameof(connectFromField));
            Ensure.IsNotNull(connectToField, nameof(connectToField));
            Ensure.IsNotNull(startWith, nameof(startWith));
            Ensure.IsNotNull(@as, nameof(@as));
            Ensure.That(from.Database.DatabaseNamespace.Equals(_collection.Database.DatabaseNamespace), "From collection must be from the same database.", nameof(from));
            Ensure.That(IsTConnectOrEnumerableTConnect <TConnectFrom, TConnect>(), "TConnectFrom must be either TConnect or a type that implements IEnumerable<TConnect>.", nameof(TConnectFrom));
            Ensure.That(IsTConnectOrEnumerableTConnect <TStartWith, TConnect>(), "TStartWith must be either TConnect or a type that implements IEnumerable<TConnect>.", nameof(TStartWith));

            const string operatorName = "$graphLookup";
            var          stage        = new DelegatedPipelineStageDefinition <TResult, TNewResult>(
                operatorName,
                (s, sr) =>
            {
                var resultSerializer         = s;
                var newResultSerializer      = options?.NewResultSerializer ?? sr.GetSerializer <TNewResult>();
                var fromSerializer           = options?.FromSerializer ?? sr.GetSerializer <TFrom>();
                var asSerializer             = options?.AsSerializer ?? sr.GetSerializer <TAs>();
                var renderedConnectToField   = connectToField.Render(fromSerializer, sr);
                var renderedStartWith        = startWith.Render(resultSerializer, sr);
                var renderedConnectFromField = connectFromField.Render(fromSerializer, sr);
                var renderedAs         = @as.Render(newResultSerializer, sr);
                var renderedDepthField = depthField?.Render(asSerializer, sr);
                var renderedRestrictSearchWithMatch = options?.RestrictSearchWithMatch?.Render(fromSerializer, sr);
                var document = new BsonDocument
                {
                    { operatorName, new BsonDocument
                      {
                          { "from", from.CollectionNamespace.CollectionName },
                          { "connectFromField", renderedConnectFromField.FieldName },
                          { "connectToField", renderedConnectToField.FieldName },
                          { "startWith", renderedStartWith },
                          { "as", renderedAs.FieldName },
                          { "depthField", () => renderedDepthField.FieldName, renderedDepthField != null },
                          { "maxDepth", () => options.MaxDepth.Value, options != null && options.MaxDepth.HasValue },
                          { "restrictSearchWithMatch", renderedRestrictSearchWithMatch, renderedRestrictSearchWithMatch != null }
                      } }
                };
                return(new RenderedPipelineStageDefinition <TNewResult>(operatorName, document, newResultSerializer));
            });

            return(AppendStage <TNewResult>(stage));
        }
Beispiel #4
0
        public override IAggregateFluent <TNewResult> Bucket <TValue, TNewResult>(
            AggregateExpressionDefinition <TResult, TValue> groupBy,
            IEnumerable <TValue> boundaries,
            ProjectionDefinition <TResult, TNewResult> output,
            AggregateBucketOptions <TValue> options = null)
        {
            Ensure.IsNotNull(groupBy, nameof(groupBy));
            Ensure.IsNotNull(boundaries, nameof(boundaries));
            Ensure.IsNotNull(output, nameof(output));

            const string operatorName = "$bucket";
            var          stage        = new DelegatedPipelineStageDefinition <TResult, TNewResult>(
                operatorName,
                (s, sr) =>
            {
                var valueSerializer         = sr.GetSerializer <TValue>();
                var newResultSerializer     = sr.GetSerializer <TNewResult>();
                var renderedGroupBy         = groupBy.Render(s, sr);
                var serializedBoundaries    = boundaries.Select(b => valueSerializer.ToBsonValue(b));
                var serializedDefaultBucket = options != null && options.DefaultBucket.HasValue ? valueSerializer.ToBsonValue(options.DefaultBucket.Value) : null;
                var renderedOutput          = output.Render(s, sr);
                var document = new BsonDocument
                {
                    { operatorName, new BsonDocument
                      {
                          { "groupBy", renderedGroupBy },
                          { "boundaries", new BsonArray(serializedBoundaries) },
                          { "default", serializedDefaultBucket, serializedDefaultBucket != null },
                          { "output", renderedOutput.Document }
                      } }
                };
                return(new RenderedPipelineStageDefinition <TNewResult>(
                           operatorName,
                           document,
                           newResultSerializer));
            });

            return(AppendStage(stage));
        }
        public override IAggregateFluent <AggregateSortByCountResult <TId> > SortByCount <TId>(AggregateExpressionDefinition <TResult, TId> id)
        {
            const string operatorName = "$sortByCount";
            var          stage        = new DelegatedPipelineStageDefinition <TResult, AggregateSortByCountResult <TId> >(
                operatorName,
                (s, sr) =>
            {
                var outputSerializer = sr.GetSerializer <AggregateSortByCountResult <TId> >();
                return(new RenderedPipelineStageDefinition <AggregateSortByCountResult <TId> >(operatorName, new BsonDocument(operatorName, id.Render(s, sr)), outputSerializer));
            });

            return(AppendStage(stage));
        }
Beispiel #6
0
        public override IAggregateFluent <TNewResult> ReplaceRoot <TNewResult>(AggregateExpressionDefinition <TResult, TNewResult> newRoot)
        {
            const string operatorName = "$replaceRoot";
            var          stage        = new DelegatedPipelineStageDefinition <TResult, TNewResult>(
                operatorName,
                (s, sr) =>
            {
                var document         = new BsonDocument(operatorName, new BsonDocument("newRoot", newRoot.Render(s, sr)));
                var outputSerializer = sr.GetSerializer <TNewResult>();
                return(new RenderedPipelineStageDefinition <TNewResult>(operatorName, document, outputSerializer));
            });

            return(AppendStage(stage));
        }