コード例 #1
0
        private void VisitSelect(SelectExpression node)
        {
            Visit(node.Source);

            var projection = AggregateProjectionTranslator.TranslateProject(node.Selector);

            _stages.Add(new BsonDocument("$project", projection));
        }
コード例 #2
0
        private void VisitGroupByWithResultSelector(GroupByWithResultSelectorExpression node)
        {
            Visit(node.Source);

            var projection = AggregateProjectionTranslator.TranslateProject(node.Selector);

            _stages.Add(new BsonDocument("$group", projection));
        }
コード例 #3
0
        /// <summary>
        /// Appends a project stage to the pipeline.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <typeparam name="TNewResult">The type of the new result.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="project">The project specifications.</param>
        /// <returns>The fluent aggregate interface.</returns>
        public static IAggregateFluent <TDocument, TNewResult> Project <TDocument, TResult, TNewResult>(this IAggregateFluent <TDocument, TResult> source, Expression <Func <TResult, TNewResult> > project)
        {
            Ensure.IsNotNull(source, "source");
            Ensure.IsNotNull(project, "projector");

            var serializer     = source.ResultSerializer ?? source.Collection.Settings.SerializerRegistry.GetSerializer <TResult>();
            var projectionInfo = AggregateProjectionTranslator.TranslateProject(project, serializer, source.Collection.Settings.SerializerRegistry);

            return(source.Project <TNewResult>(projectionInfo.Projection, projectionInfo.Serializer));
        }
コード例 #4
0
        /// <summary>
        /// Appends a group stage to the pipeline.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TNewResult">The type of the new result.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="idProjector">The identifier projector.</param>
        /// <param name="groupProjector">The group projector.</param>
        /// <returns>The fluent aggregate interface.</returns>
        public static IAggregateFluent <TDocument, TNewResult> Group <TDocument, TResult, TKey, TNewResult>(this IAggregateFluent <TDocument, TResult> source, Expression <Func <TResult, TKey> > idProjector, Expression <Func <IGrouping <TKey, TResult>, TNewResult> > groupProjector)
        {
            Ensure.IsNotNull(source, "source");
            Ensure.IsNotNull(idProjector, "idProjector");
            Ensure.IsNotNull(groupProjector, "groupProjector");

            var serializer     = source.ResultSerializer ?? source.Collection.Settings.SerializerRegistry.GetSerializer <TResult>();
            var projectionInfo = AggregateProjectionTranslator.TranslateGroup <TKey, TResult, TNewResult>(idProjector, groupProjector, serializer, source.Collection.Settings.SerializerRegistry);

            return(source.Group <TNewResult>(projectionInfo.Projection, projectionInfo.Serializer));
        }
        private async Task <ProjectedResult <TResult> > Group <TKey, TResult>(Expression <Func <Root, TKey> > idProjector, Expression <Func <IGrouping <TKey, Root>, TResult> > groupProjector)
        {
            var serializer     = BsonSerializer.SerializerRegistry.GetSerializer <Root>();
            var projectionInfo = AggregateProjectionTranslator.TranslateGroup <TKey, Root, TResult>(idProjector, groupProjector, serializer, BsonSerializer.SerializerRegistry);

            var pipelineOperator = new BsonDocument("$group", projectionInfo.Document);

            using (var cursor = await _collection.AggregateAsync <TResult>(new PipelineStagePipelineDefinition <Root, TResult>(new PipelineStageDefinition <Root, TResult>[] { pipelineOperator }, projectionInfo.ProjectionSerializer)))
            {
                var list = await cursor.ToListAsync();

                return(new ProjectedResult <TResult>
                {
                    Projection = projectionInfo.Document,
                    Value = (TResult)list[0]
                });
            }
        }
コード例 #6
0
        private void VisitSelectMany(SelectManyExpression node)
        {
            Visit(node.Source);

            var field = node.CollectionSelector as ISerializationExpression;

            if (field == null || field.SerializationInfo.ElementName == null)
            {
                var message = string.Format("The collection selector must be an ISerializationExpression: {0}", node.ToString());
                throw new NotSupportedException(message);
            }

            _stages.Add(new BsonDocument("$unwind", "$" + field.SerializationInfo.ElementName));

            var projection = AggregateProjectionTranslator.TranslateProject(node.ResultSelector);

            _stages.Add(new BsonDocument("$project", projection));
        }
        private async Task <ProjectedResult <T> > Project <T>(Expression <Func <Root, T> > projector)
        {
            var serializer     = BsonSerializer.SerializerRegistry.GetSerializer <Root>();
            var projectionInfo = AggregateProjectionTranslator.TranslateProject <Root, T>(projector, serializer, BsonSerializer.SerializerRegistry);

            var pipelineOperator = new BsonDocument("$project", projectionInfo.Projection);
            var options          = new AggregateOptions <T> {
                ResultSerializer = projectionInfo.Serializer
            };

            using (var cursor = await _collection.AggregateAsync <T>(new object[] { pipelineOperator }, options))
            {
                var list = await cursor.ToListAsync();

                return(new ProjectedResult <T>
                {
                    Projection = projectionInfo.Projection,
                    Value = (T)list[0]
                });
            }
        }
コード例 #8
0
 public override RenderedProjectionDefinition <TNewResult> Render(IBsonSerializer <TResult> documentSerializer, IBsonSerializerRegistry serializerRegistry)
 {
     return(AggregateProjectionTranslator.TranslateGroup <TKey, TResult, TNewResult>(_idExpression, _groupExpression, documentSerializer, serializerRegistry));
 }