Beispiel #1
0
            public override RenderedPipelineStageDefinition <IGrouping <TKey, TInput> > Render(
                IBsonSerializer <TInput> inputSerializer,
                IBsonSerializerRegistry serializerRegistry,
                LinqProvider linqProvider)
            {
                if (linqProvider != LinqProvider.V3)
                {
                    throw new InvalidOperationException("GroupExpressionStageDefinitions can only be used with LINQ3.");
                }

                var expression = CreateExpression(inputSerializer);

                expression = PartialEvaluator.EvaluatePartially(expression);
                var context             = TranslationContext.Create(expression, inputSerializer);
                var unoptimizedPipeline = ExpressionToPipelineTranslator.Translate(context, expression);
                var pipeline            = AstPipelineOptimizer.Optimize(unoptimizedPipeline);

                var groupStageDocument = pipeline.Stages[0].Render().AsBsonDocument;
                var renderedGroupStage = new RenderedPipelineStageDefinition <IGrouping <TKey, TInput> >("$group", groupStageDocument, new DummyIGroupingSerializer());

                var projectStageDocument = pipeline.Stages[1].Render().AsBsonDocument;

                _renderedProjectStage = new RenderedPipelineStageDefinition <TOutput>("$project", projectStageDocument, (IBsonSerializer <TOutput>)pipeline.OutputSerializer);

                return(renderedGroupStage);
            }
Beispiel #2
0
        private ProjectedResult <TResult> Group <TKey, TResult>(Expression <Func <Root, TKey> > idProjector, Expression <Func <IGrouping <TKey, Root>, TResult> > groupProjector, ExpressionTranslationOptions translationOptions)
        {
            var queryable = __collection.AsQueryable()
                            .GroupBy(idProjector)
                            .Select(groupProjector);

            var collectionSerializer = (IBsonDocumentSerializer)BsonSerializer.LookupSerializer <Root>();
            var context  = TranslationContext.Create(queryable.Expression, collectionSerializer);
            var pipeline = ExpressionToPipelineTranslator.Translate(context, queryable.Expression);

            pipeline = AstPipelineOptimizer.Optimize(pipeline);

            var stages = pipeline.Stages.Select(s => s.Render()).Cast <BsonDocument>().ToList();

            stages.Insert(1, new BsonDocument("$sort", new BsonDocument("_id", 1))); // force a standard order for testing purposes
            var pipelineDefinition = new BsonDocumentStagePipelineDefinition <Root, TResult>(stages, outputSerializer: (IBsonSerializer <TResult>)pipeline.OutputSerializer);
            var results            = __collection.Aggregate(pipelineDefinition).ToList();

            stages.RemoveAt(1); // remove $sort added above for predictable testing
            return(new ProjectedResult <TResult>
            {
                Stages = stages,
                Value = results[0]
            });
        }
        public static ExecutableQuery <TDocument, TOutput, TResult> Create <TDocument, TOutput, TResult>(
            IMongoCollection <TDocument> collection,
            AggregateOptions options,
            AstPipeline unoptimizedPipeline,
            IExecutableQueryFinalizer <TOutput, TResult> finalizer)
        {
            var pipeline = AstPipelineOptimizer.Optimize(unoptimizedPipeline);

            return(new ExecutableQuery <TDocument, TOutput, TResult>(collection, options, unoptimizedPipeline, pipeline, finalizer));
        }