MongoDB
 sql >> Baza danych >  >> NoSQL >> MongoDB

Jak serializować typy wartości za pomocą serializatora MongoDB C#?

Możesz utworzyć niestandardowy serializator do obsługi struktur za pomocą tego kodu:

public class StructBsonSerializer : IBsonSerializer 
{ 
    public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) 
    { 
        var fields = nominalType.GetFields(BindingFlags.Instance | BindingFlags.Public); 
        var propsAll = nominalType.GetProperties(BindingFlags.Instance | BindingFlags.Public); 

        var props = new List<PropertyInfo>(); 
        foreach (var prop in propsAll) 
        { 
            if (prop.CanWrite) 
            { 
                props.Add(prop); 
            } 
        } 

        bsonWriter.WriteStartDocument(); 

        foreach (var field in fields) 
        { 
            bsonWriter.WriteName(field.Name); 
            BsonSerializer.Serialize(bsonWriter, field.FieldType, field.GetValue(value)); 
        } 
        foreach (var prop in props) 
        { 
            bsonWriter.WriteName(prop.Name); 
            BsonSerializer.Serialize(bsonWriter, prop.PropertyType, prop.GetValue(value, null)); 
        } 

        bsonWriter.WriteEndDocument(); 
    } 

    public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) 
    { 
        var obj = Activator.CreateInstance(actualType); 

        bsonReader.ReadStartDocument(); 

        while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) 
        { 
            var name = bsonReader.ReadName(); 

            var field = actualType.GetField(name); 
            if (field != null) 
            { 
                var value = BsonSerializer.Deserialize(bsonReader, field.FieldType); 
                field.SetValue(obj, value); 
            } 

            var prop = actualType.GetProperty(name); 
            if (prop != null) 
            { 
                var value = BsonSerializer.Deserialize(bsonReader, prop.PropertyType); 
                prop.SetValue(obj, value, null); 
            } 
        } 

        bsonReader.ReadEndDocument(); 

        return obj; 
    } 

    public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) 
    { 
        return Deserialize(bsonReader, nominalType, nominalType, options); 
    } 

    public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator) 
    { 
        throw new NotImplementedException(); 
    } 

    public void SetDocumentId(object document, object id) 
    { 
        throw new NotImplementedException(); 
    } 
} 

Następnie zarejestruj ten serializator dla swojej struktury:

BsonSerializer.RegisterSerializer(typeof(MyStruct), new StructBsonSerializer());


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Jak dołączyć do dwóch kolekcji w mongo bez wyszukiwania?

  2. Znajdź element na podstawie dwóch wartości

  3. Znajdź wszystkie dokumenty, w których pole nie istnieje, a jeśli pole istnieje, zastosuj warunek

  4. Użyj $cond w $match w agregacji mongoDB

  5. ClusterControl — wszystkie najważniejsze funkcje i ulepszenia z 2017 roku