コード例 #1
0
        public override IAggregateFluent <TNewResult> Unwind <TNewResult>(FieldDefinition <TResult> field, AggregateUnwindOptions <TNewResult> options)
        {
            options = options ?? new AggregateUnwindOptions <TNewResult>();

            const string operatorName = "$unwind";
            var          stage        = new DelegatedPipelineStageDefinition <TResult, TNewResult>(
                operatorName,
                (s, sr) =>
            {
                var newResultSerializer = options.ResultSerializer ?? (s as IBsonSerializer <TNewResult>) ?? sr.GetSerializer <TNewResult>();

                var fieldName = "$" + field.Render(s, sr).FieldName;
                string includeArrayIndexFieldName = null;
                if (options.IncludeArrayIndex != null)
                {
                    includeArrayIndexFieldName = options.IncludeArrayIndex.Render(newResultSerializer, sr).FieldName;
                }

                BsonValue value = fieldName;
                if (options.PreserveNullAndEmptyArrays.HasValue || includeArrayIndexFieldName != null)
                {
                    value = new BsonDocument
                    {
                        { "path", fieldName },
                        { "preserveNullAndEmptyArrays", options.PreserveNullAndEmptyArrays, options.PreserveNullAndEmptyArrays.HasValue },
                        { "includeArrayIndex", includeArrayIndexFieldName, includeArrayIndexFieldName != null }
                    };
                }
                return(new RenderedPipelineStageDefinition <TNewResult>(
                           operatorName,
                           new BsonDocument(operatorName, value),
                           newResultSerializer));
            });

            return(AppendStage <TNewResult>(stage));
        }
コード例 #2
0
        public override IAggregateFluent <TNewResult> BucketAuto <TValue, TNewResult>(
            AggregateExpressionDefinition <TResult, TValue> groupBy,
            int buckets,
            ProjectionDefinition <TResult, TNewResult> output,
            AggregateBucketAutoOptions options = null)
        {
            Ensure.IsNotNull(groupBy, nameof(groupBy));
            Ensure.IsGreaterThanZero(buckets, nameof(buckets));
            Ensure.IsNotNull(output, nameof(output));

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

            return(AppendStage(stage));
        }