private void TranslateOrderBy(OrderByExpression node)
        {
            Translate(node.Source);

            BsonDocument sort = new BsonDocument();

            foreach (var clause in node.Clauses)
            {
                var field = FieldExpressionFlattener.FlattenFields(clause.Expression) as IFieldExpression;
                if (field == null)
                {
                    throw new NotSupportedException("Only fields are allowed in a $sort.");
                }

                var direction = clause.Direction == SortDirection.Ascending ? 1 : -1;
                if (sort.Contains(field.FieldName))
                {
                    var message = string.Format("Redundant ordering fields are not supported: {0}.", field.FieldName);
                    throw new NotSupportedException(message);
                }

                sort.Add(field.FieldName, direction);
            }

            _stages.Add(new BsonDocument("$sort", sort));
        }
        public static RenderedProjectionDefinition <TProjection> Translate <TDocument, TProjection>(Expression <Func <TDocument, TProjection> > projector, IBsonSerializer <TDocument> parameterSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            var bindingContext     = new PipelineBindingContext(serializerRegistry);
            var documentExpression = new DocumentExpression(parameterSerializer);

            bindingContext.AddExpressionMapping(projector.Parameters[0], documentExpression);

            var node = PartialEvaluator.Evaluate(projector.Body);

            node = Transformer.Transform(node);
            node = bindingContext.Bind(node, isClientSideProjection: true);
            node = FieldExpressionFlattener.FlattenFields(node);

            BsonDocument projectionDocument = null;
            IBsonSerializer <TProjection> serializer;

            if (node is DocumentExpression)
            {
                serializer = new ProjectingDeserializer <TDocument, TProjection>(parameterSerializer, projector.Compile());
            }
            else
            {
                var candidateFields   = SerializationExpressionGatherer.Gather(node);
                var fields            = GetUniqueFieldsByHierarchy(candidateFields);
                var serializationInfo = fields.Select(x => new BsonSerializationInfo(x.FieldName, x.Serializer, x.Serializer.ValueType)).ToList();

                var projectedObjectExpression = Expression.Parameter(typeof(ProjectedObject), "document");
                var translator = new FindProjectionTranslator(documentExpression, projectedObjectExpression, fields);
                var translated = translator.Visit(node);
                if (translator._fullDocument)
                {
                    serializer = new ProjectingDeserializer <TDocument, TProjection>(parameterSerializer, projector.Compile());
                }
                else
                {
                    var newProjector = Expression.Lambda <Func <ProjectedObject, TProjection> >(
                        translated,
                        projectedObjectExpression);

                    projectionDocument = GetProjectionDocument(serializationInfo);
                    var projectedObjectSerializer = new ProjectedObjectDeserializer(serializationInfo);
                    serializer = new ProjectingDeserializer <ProjectedObject, TProjection>(projectedObjectSerializer, newProjector.Compile());
                }
            }

            return(new RenderedProjectionDefinition <TProjection>(projectionDocument, serializer));
        }