Example #1
0
        public override IAggregateFluent <TNewResult> Group <TNewResult>(ProjectionDefinition <TResult, TNewResult> group)
        {
            const string operatorName = "$group";
            var          stage        = new DelegatedPipelineStageDefinition <TResult, TNewResult>(
                operatorName,
                (s, sr) =>
            {
                var renderedProjection = group.Render(s, sr);
                return(new RenderedPipelineStageDefinition <TNewResult>(operatorName, new BsonDocument(operatorName, renderedProjection.Document), renderedProjection.ProjectionSerializer));
            });

            return(AppendStage <TNewResult>(stage));
        }
        public override IAggregateFluent <TNewResult> Project <TNewResult>(ProjectionDefinition <TResult, TNewResult> projection)
        {
            const string operatorName = "$project";
            var          stage        = new DelegatedPipelineStageDefinition <TResult, TNewResult>(
                operatorName,
                (s, sr) =>
            {
                var renderedProjection = projection.Render(s, sr);
                BsonDocument document;
                if (renderedProjection.Document == null)
                {
                    document = new BsonDocument();
                }
                else
                {
                    document = new BsonDocument(operatorName, renderedProjection.Document);
                }
                return(new RenderedPipelineStageDefinition <TNewResult>(operatorName, document, renderedProjection.ProjectionSerializer));
            });

            return(AppendStage <TNewResult>(stage));
        }
Example #3
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));
        }
Example #4
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));
        }
Example #5
0
        /// <summary>
        /// Projects the result.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
        /// <param name="find">The fluent find.</param>
        /// <param name="projection">The projection.</param>
        /// <returns>The fluent find interface.</returns>
        public static IFindFluent <TDocument, BsonDocument> Project <TDocument, TProjection>(this IFindFluent <TDocument, TProjection> find, ProjectionDefinition <TDocument, BsonDocument> projection)
        {
            Ensure.IsNotNull(find, nameof(find));
            Ensure.IsNotNull(projection, nameof(projection));

            return(find.Project <BsonDocument>(projection));
        }
Example #6
0
 /// <inheritdoc />
 public abstract IAggregateFluent <TNewResult> Group <TNewResult>(ProjectionDefinition <TResult, TNewResult> group);
Example #7
0
        public override IFindFluent <TDocument, TNewProjection> Project <TNewProjection>(ProjectionDefinition <TDocument, TNewProjection> projection)
        {
            var newOptions = new FindOptions <TDocument, TNewProjection>
            {
                AllowDiskUse        = _options.AllowDiskUse,
                AllowPartialResults = _options.AllowPartialResults,
                BatchSize           = _options.BatchSize,
                Collation           = _options.Collation,
                Comment             = _options.Comment,
                CursorType          = _options.CursorType,
                Hint         = _options.Hint,
                Limit        = _options.Limit,
                Max          = _options.Max,
                MaxAwaitTime = _options.MaxAwaitTime,
                MaxTime      = _options.MaxTime,
                Min          = _options.Min,
#pragma warning disable 618
                Modifiers = _options.Modifiers,
#pragma warning restore 618
                NoCursorTimeout = _options.NoCursorTimeout,
#pragma warning disable 618
                OplogReplay = _options.OplogReplay,
#pragma warning restore 618
                Projection   = projection,
                ReturnKey    = _options.ReturnKey,
                ShowRecordId = _options.ShowRecordId,
                Skip         = _options.Skip,
                Sort         = _options.Sort,
            };

            return(new FindFluent <TDocument, TNewProjection>(_session, _collection, _filter, newOptions));
        }
 /// <summary>
 /// Appends a project stage to the pipeline.
 /// </summary>
 /// <typeparam name="TResult">The type of the result.</typeparam>
 /// <param name="aggregate">The aggregate.</param>
 /// <param name="projection">The projection.</param>
 /// <returns>
 /// The fluent aggregate interface.
 /// </returns>
 public static IAggregateFluent <BsonDocument> Project <TResult>(this IAggregateFluent <TResult> aggregate, ProjectionDefinition <TResult, BsonDocument> projection)
 {
     Ensure.IsNotNull(aggregate, nameof(aggregate));
     return(aggregate.AppendStage(PipelineStageDefinitionBuilder.Project(projection)));
 }
Example #9
0
        /// <summary>
        /// Combines an existing projection with a projection that filters the contents of an array.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <typeparam name="TItem">The type of the item.</typeparam>
        /// <param name="projection">The projection.</param>
        /// <param name="field">The field.</param>
        /// <param name="filter">The filter.</param>
        /// <returns>
        /// A combined projection.
        /// </returns>
        public static ProjectionDefinition <TDocument> ElemMatch <TDocument, TItem>(this ProjectionDefinition <TDocument> projection, Expression <Func <TDocument, IEnumerable <TItem> > > field, Expression <Func <TItem, bool> > filter)
        {
            var builder = Builders <TDocument> .Projection;

            return(builder.Combine(projection, builder.ElemMatch(field, filter)));
        }
Example #10
0
        /// <summary>
        /// Combines an existing projection with a projection that excludes a field.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <param name="projection">The projection.</param>
        /// <param name="field">The field.</param>
        /// <returns>
        /// A combined projection.
        /// </returns>
        public static ProjectionDefinition <TDocument> Exclude <TDocument>(this ProjectionDefinition <TDocument> projection, FieldDefinition <TDocument> field)
        {
            var builder = Builders <TDocument> .Projection;

            return(builder.Combine(projection, builder.Exclude(field)));
        }
Example #11
0
 /// <inheritdoc />
 public abstract IFindFluent <TDocument, TNewProjection> Project <TNewProjection>(ProjectionDefinition <TDocument, TNewProjection> projection);
        /// <summary>
        /// Appends a group stage to the pipeline.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="aggregate">The aggregate.</param>
        /// <param name="group">The group projection.</param>
        /// <returns>
        /// The fluent aggregate interface.
        /// </returns>
        public static IAggregateFluent <BsonDocument> Group <TResult>(this IAggregateFluent <TResult> aggregate, ProjectionDefinition <TResult, BsonDocument> group)
        {
            Ensure.IsNotNull(aggregate, "aggregate");
            Ensure.IsNotNull(group, "group");

            return(aggregate.Group <BsonDocument>(group));
        }
Example #13
0
        /// <summary>
        /// Combines an existing projection with a meta projection.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <param name="projection">The projection.</param>
        /// <param name="field">The field.</param>
        /// <param name="metaFieldName">The meta field name.</param>
        /// <returns>
        /// A combined projection.
        /// </returns>
        public static ProjectionDefinition <TDocument> Meta <TDocument>(this ProjectionDefinition <TDocument> projection, string field, string metaFieldName)
        {
            var builder = Builders <TDocument> .Projection;

            return(builder.Combine(projection, builder.Meta(field, metaFieldName)));
        }
 public override IAggregateFluent <TNewResult> Group <TNewResult>(ProjectionDefinition <TResult, TNewResult> group)
 {
     return(WithPipeline(_pipeline.Group(group)));
 }
Example #15
0
        /// <summary>
        /// Combines an existing projection with a projection that includes a field.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <param name="projection">The projection.</param>
        /// <param name="field">The field.</param>
        /// <returns>
        /// A combined projection.
        /// </returns>
        public static ProjectionDefinition <TDocument> Include <TDocument>(this ProjectionDefinition <TDocument> projection, Expression <Func <TDocument, object> > field)
        {
            var builder = Builders <TDocument> .Projection;

            return(builder.Combine(projection, builder.Include(field)));
        }
 public KnownResultTypeProjectionDefinitionAdapter(ProjectionDefinition <TSource> projection, IBsonSerializer <TProjection> projectionSerializer = null)
 {
     _projection           = Ensure.IsNotNull(projection, "projection");
     _projectionSerializer = projectionSerializer;
 }
        /// <summary>
        /// Appends a project stage to the pipeline.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="aggregate">The aggregate.</param>
        /// <param name="projection">The projection.</param>
        /// <returns>
        /// The fluent aggregate interface.
        /// </returns>
        public static IAggregateFluent <BsonDocument> Project <TResult>(this IAggregateFluent <TResult> aggregate, ProjectionDefinition <TResult, BsonDocument> projection)
        {
            Ensure.IsNotNull(aggregate, "aggregate");
            Ensure.IsNotNull(projection, "projection");

            return(aggregate.Project <BsonDocument>(projection));
        }
Example #18
0
 /// <inheritdoc />
 public abstract IAggregateFluent <TNewResult> Project <TNewResult>(ProjectionDefinition <TResult, TNewResult> projection);
Example #19
0
        /// <summary>
        /// Combines an existing projection with a projection that filters the contents of an array.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <typeparam name="TItem">The type of the item.</typeparam>
        /// <param name="projection">The projection.</param>
        /// <param name="field">The field.</param>
        /// <param name="filter">The filter.</param>
        /// <returns>
        /// A combined projection.
        /// </returns>
        public static ProjectionDefinition <TDocument> ElemMatch <TDocument, TItem>(this ProjectionDefinition <TDocument> projection, FieldDefinition <TDocument> field, FilterDefinition <TItem> filter)
        {
            var builder = Builders <TDocument> .Projection;

            return(builder.Combine(projection, builder.ElemMatch(field, filter)));
        }
Example #20
0
        /// <summary>
        /// Combines an existing projection with a text score projection.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <param name="projection">The projection.</param>
        /// <param name="field">The field.</param>
        /// <returns>
        /// A combined projection.
        /// </returns>
        public static ProjectionDefinition <TDocument> MetaTextScore <TDocument>(this ProjectionDefinition <TDocument> projection, string field)
        {
            var builder = Builders <TDocument> .Projection;

            return(builder.Combine(projection, builder.MetaTextScore(field)));
        }
Example #21
0
        public override IFindFluent <TDocument, TNewProjection> Project <TNewProjection>(ProjectionDefinition <TDocument, TNewProjection> projection)
        {
            var newOptions = new FindOptions <TDocument, TNewProjection>
            {
                AllowPartialResults = _options.AllowPartialResults,
                BatchSize           = _options.BatchSize,
                Collation           = _options.Collation,
                Comment             = _options.Comment,
                CursorType          = _options.CursorType,
                Limit           = _options.Limit,
                MaxAwaitTime    = _options.MaxAwaitTime,
                MaxTime         = _options.MaxTime,
                Modifiers       = _options.Modifiers,
                NoCursorTimeout = _options.NoCursorTimeout,
                OplogReplay     = _options.OplogReplay,
                Projection      = projection,
                Skip            = _options.Skip,
                Sort            = _options.Sort,
            };

            return(new FindFluent <TDocument, TNewProjection>(_session, _collection, _filter, newOptions));
        }
 public override IAggregateFluent <TNewResult> Project <TNewResult>(ProjectionDefinition <TResult, TNewResult> projection)
 {
     return(WithPipeline(_pipeline.Project(projection)));
 }
Example #23
0
        /// <summary>
        /// Combines an existing projection with an array slice projection.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <param name="projection">The projection.</param>
        /// <param name="field">The field.</param>
        /// <param name="skip">The skip.</param>
        /// <param name="limit">The limit.</param>
        /// <returns>
        /// A combined projection.
        /// </returns>
        public static ProjectionDefinition <TDocument> Slice <TDocument>(this ProjectionDefinition <TDocument> projection, Expression <Func <TDocument, object> > field, int skip, int?limit = null)
        {
            var builder = Builders <TDocument> .Projection;

            return(builder.Combine(projection, builder.Slice(field, skip, limit)));
        }