Ejemplo n.º 1
0
        public static RenderedProjectionDefinition <TResult> TranslateGroup <TKey, TDocument, TResult>(Expression <Func <TDocument, TKey> > idProjector, Expression <Func <IGrouping <TKey, TDocument>, TResult> > groupProjector, IBsonSerializer <TDocument> parameterSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            var keyBinder          = new SerializationInfoBinder(serializerRegistry);
            var boundKeyExpression = BindSerializationInfo(keyBinder, idProjector, parameterSerializer);

            if (!(boundKeyExpression is ISerializationExpression))
            {
                var keySerializer = SerializerBuilder.Build(boundKeyExpression, serializerRegistry);
                boundKeyExpression = new SerializationExpression(
                    boundKeyExpression,
                    new BsonSerializationInfo(null, keySerializer, typeof(TKey)));
            }

            var idExpression = new GroupIdExpression(boundKeyExpression, ((ISerializationExpression)boundKeyExpression).SerializationInfo);

            var groupBinder = new AccumulatorBinder(serializerRegistry);

            groupBinder.RegisterMemberReplacement(typeof(IGrouping <TKey, TDocument>).GetProperty("Key"), idExpression);
            var groupSerializer      = new ArraySerializer <TDocument>(parameterSerializer);
            var boundGroupExpression = BindSerializationInfo(groupBinder, groupProjector, groupSerializer);
            var projectionSerializer = (IBsonSerializer <TResult>)SerializerBuilder.Build(boundGroupExpression, serializerRegistry);
            var projection           = AggregateLanguageTranslator.Translate(boundGroupExpression).AsBsonDocument;

            // must have an "_id" in a group document
            if (!projection.Contains("_id"))
            {
                var idProjection = AggregateLanguageTranslator.Translate(boundKeyExpression);
                projection.InsertAt(0, new BsonElement("_id", idProjection));
            }

            return(new RenderedProjectionDefinition <TResult>(projection, projectionSerializer));
        }
        private BsonDocument TranslateProjectValue(Expression selector)
        {
            BsonDocument projectValue;
            var          result = AggregateLanguageTranslator.Translate(selector);

            if (result.BsonType == BsonType.String)
            {
                // this means we got back a field expression prefixed with a $ sign.
                projectValue = new BsonDocument(result.ToString().Substring(1), 1);
            }
            else if (result.BsonType == BsonType.Document)
            {
                projectValue = (BsonDocument)result;
            }
            else
            {
                throw new NotSupportedException($"The expression {selector.ToString()} is not supported.");
            }

            if (!projectValue.Contains("_id"))
            {
                projectValue.Add("_id", 0);
            }

            return(projectValue);
        }
        private BsonDocument TranslateProjectValue(Expression selector)
        {
            BsonDocument projectValue;

            if (selector is FieldExpression) // not IFieldExpression
            {
                projectValue = new BsonDocument(((IFieldExpression)selector).FieldName, 1);
            }
            else
            {
                var result = AggregateLanguageTranslator.Translate(selector);
                if (result.BsonType == BsonType.String)
                {
                    projectValue = new BsonDocument(result.ToString(), 1);
                }
                else if (result.BsonType == BsonType.Document)
                {
                    projectValue = (BsonDocument)result;
                }
                else
                {
                    throw new NotSupportedException();
                }
            }

            if (!projectValue.Contains("_id"))
            {
                projectValue.Add("_id", 0);
            }

            return(projectValue);
        }
Ejemplo n.º 4
0
 public static BsonValue Translate<TSource, TResult>(
     Expression<Func<TSource, TResult>> expression,
     IBsonSerializer<TSource> sourceSerializer,
     IBsonSerializerRegistry serializerRegistry,
     ExpressionTranslationOptions translationOptions)
 {
     var bindingContext = new PipelineBindingContext(serializerRegistry);
     var boundExpression = BindResult(bindingContext, expression, sourceSerializer);
     return AggregateLanguageTranslator.Translate(boundExpression, translationOptions);
 }
        public static BsonDocument TranslateProject(Expression expression)
        {
            var projection = (BsonDocument)AggregateLanguageTranslator.Translate(expression);

            if (!projection.Contains("_id"))
            {
                projection.Add("_id", 0);
            }

            return(projection);
        }
        private void TranslateGroupBy(GroupByExpression node)
        {
            Translate(node.Source);

            var groupValue = new BsonDocument();
            var idValue    = AggregateLanguageTranslator.Translate(node.KeySelector);

            groupValue.Add("_id", idValue);
            foreach (var accumulator in node.Accumulators)
            {
                var accumulatorValue = AggregateLanguageTranslator.Translate(accumulator);
                groupValue.Add(accumulator.FieldName, accumulatorValue);
            }

            _stages.Add(new BsonDocument("$group", groupValue));
        }
        public static RenderedProjectionDefinition <TResult> Translate <TKey, TDocument, TResult>(Expression <Func <TDocument, TKey> > idProjector, Expression <Func <IGrouping <TKey, TDocument>, TResult> > groupProjector, IBsonSerializer <TDocument> parameterSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            var bindingContext = new PipelineBindingContext(serializerRegistry);

            var keySelector          = BindKeySelector(bindingContext, idProjector, parameterSerializer);
            var boundGroupExpression = BindGroup(bindingContext, groupProjector, parameterSerializer, keySelector);

            var projectionSerializer = bindingContext.GetSerializer(boundGroupExpression.Type, boundGroupExpression);
            var projection           = AggregateLanguageTranslator.Translate(boundGroupExpression).AsBsonDocument;

            // must have an "_id" in a group document
            if (!projection.Contains("_id"))
            {
                var idProjection = AggregateLanguageTranslator.Translate(keySelector);
                projection.InsertAt(0, new BsonElement("_id", idProjection));
            }

            return(new RenderedProjectionDefinition <TResult>(projection, (IBsonSerializer <TResult>)projectionSerializer));
        }
        private void TranslateJoin(JoinExpression node)
        {
            Translate(node.Source);

            var joined = node.Joined as CollectionExpression;

            if (joined == null)
            {
                throw new NotSupportedException("Only a collection is allowed to be joined.");
            }

            var localFieldValue = AggregateLanguageTranslator.Translate(node.SourceKeySelector);

            if (localFieldValue.BsonType != BsonType.String)
            {
                throw new NotSupportedException("Could not translate the local field.");
            }

            var localField = localFieldValue.ToString().Substring(1); // remove '$'

            var foreignFieldValue = AggregateLanguageTranslator.Translate(node.SourceKeySelector);

            if (foreignFieldValue.BsonType != BsonType.String)
            {
                throw new NotSupportedException("Could not translate the foreign field.");
            }

            var foreignField = foreignFieldValue.ToString().Substring(1); // remove '$'

            _stages.Add(new BsonDocument("$lookup", new BsonDocument
            {
                { "from", ((CollectionExpression)node.Joined).CollectionNamespace.CollectionName },
                { "localField", localField },
                { "foreignField", foreignField },
                { "as", node.JoinedItemName }
            }));

            _stages.Add(new BsonDocument("$unwind", "$" + node.JoinedItemName));
        }
Ejemplo n.º 9
0
        public static BsonDocument TranslateProject(Expression expression)
        {
            var value      = AggregateLanguageTranslator.Translate(expression);
            var projection = value as BsonDocument;

            if (projection == null)
            {
                projection = new BsonDocument(value.ToString().Substring(1), 1);
            }
            else if (expression.NodeType != ExpressionType.New && expression.NodeType != ExpressionType.MemberInit)
            {
                // this means we are scalar projection
                projection = new BsonDocument("__fld0", value);
            }

            if (!projection.Contains("_id"))
            {
                projection.Add("_id", 0);
            }

            return(projection);
        }
 public static BsonValue Translate(Expression node)
 {
     var builder = new AggregateLanguageTranslator();
     return builder.TranslateValue(node);
 }
Ejemplo n.º 11
0
        public static BsonValue Translate(Expression node, ExpressionTranslationOptions translationOptions)
        {
            var builder = new AggregateLanguageTranslator(translationOptions);

            return(builder.TranslateValue(node));
        }
        public static BsonValue Translate(Expression node)
        {
            var builder = new AggregateLanguageTranslator();

            return(builder.BuildValue(node));
        }