public override BsonDocument Render(
            IBsonSerializer documentSerializer,
            IBsonSerializerRegistry serializerRegistry)
        {
            StringFieldDefinitionHelper.Resolve(
                _path,
                documentSerializer,
                out string?resolvedFieldName,
                out IBsonSerializer? _);

            BsonValue value;

            switch (_direction)
            {
            case SortDirection.Ascending:
                value = 1;
                break;

            case SortDirection.Descending:
                value = -1;
                break;

            default:
                throw new InvalidOperationException(
                          "Unknown value for " + typeof(SortDirection) + ".");
            }

            return(new BsonDocument(resolvedFieldName ?? _path, value));
        }
Beispiel #2
0
        public override BsonDocument Render(
            IBsonSerializer documentSerializer,
            IBsonSerializerRegistry serializerRegistry)
        {
            StringFieldDefinitionHelper.Resolve(
                _path,
                documentSerializer,
                out string?resolvedFieldName,
                out IBsonSerializer? resolvedFieldSerializer);

            resolvedFieldSerializer ??= documentSerializer;

            if (_value is BsonDocument bsonDocument)
            {
                return(new BsonDocument(resolvedFieldName, bsonDocument));
            }

            if (_value is BsonValue bsonValue)
            {
                return(new BsonDocument(resolvedFieldName, bsonValue));
            }

            if (_value is MongoDbFilterDefinition mongoDbOperation)
            {
                if (_path is "")
                {
                    return(mongoDbOperation.Render(resolvedFieldSerializer, serializerRegistry));
                }

                return(new BsonDocument(
                           resolvedFieldName,
                           mongoDbOperation.Render(resolvedFieldSerializer, serializerRegistry)));
            }

            var document = new BsonDocument();

            using var bsonWriter = new BsonDocumentWriter(document);
            var context = BsonSerializationContext.CreateRoot(bsonWriter);

            bsonWriter.WriteStartDocument();
            bsonWriter.WriteName(resolvedFieldName);
            if (_value is IList values)
            {
                bsonWriter.WriteStartArray();
                foreach (var value in values)
                {
                    resolvedFieldSerializer.Serialize(context, value);
                }

                bsonWriter.WriteEndArray();
            }
            else
            {
                resolvedFieldSerializer.Serialize(context, _value);
            }

            bsonWriter.WriteEndDocument();

            return(document);
        }
Beispiel #3
0
        public override BsonDocument Render(
            IBsonSerializer documentSerializer,
            IBsonSerializerRegistry serializerRegistry)
        {
            StringFieldDefinitionHelper.Resolve(
                _path,
                documentSerializer,
                out string?resolvedFieldName,
                out IBsonSerializer? _);

            return(new BsonDocument(resolvedFieldName ?? _path, 1));
        }