Search code examples
c#.netmongodbprotobuf-netmongodb-.net-driver

Generic List is not being inserted into MongoDB


I'm trying to insert a protobuf-net object into my MongoDB. The object looks something like this:

message Measurement
{
    optional string                 _someString1            = 1 [default = ""];
    optional string                 _someString2            = 2 [default = ""];
    repeated MyMessage1             _myMessages1            = 3;
    repeated MyMessage2             _myMessages2            = 4;
    repeated MyMessage3             _myMessages3            = 5;
}

MyMessage1..3 are messages in the same proto file and they contain a few strings, doubles and int64s. I fill an instance of Measurement with some data and try to insert it into my DB, like this:

var col = MyDatabase.GetCollection<Measurement>("Measurements");
col.Insert(instanceOfMeasurement);

Now, if I check on the contents of the DB, I can see that the instanceOfMeasurement has been added correctly. But the repeated fields within haven't. Do I need to prepare a more complex object in some way, before stuffing it into MongoDB?

Okay, here is what protobuf-net creates:

[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Measurement")]
public partial class Measurement : global::ProtoBuf.IExtensible, global::System.ComponentModel.INotifyPropertyChanged
{
public Measurement() {}

private string __someString1;
[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"_someString1", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string _SomeString1
{
  get { return __someString1?? ""; }
  set { __someString1 = value; OnPropertyChanged(@"_someString1"); }
}
[global::System.Xml.Serialization.XmlIgnore]
[global::System.ComponentModel.Browsable(false)]
public bool _someString1Specified
{
  get { return this.__someString1 != null; }
  set { if (value == (this.__someString1== null)) this.__someString1 = value ? this._someString1 : (string)null; }
}
private bool ShouldSerialize_someString1() { return _someString1Specified; }
private void Reset_someString1() { _someString1Specified = false; }

private string __someString2;
[global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"_someString2", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string _SomeString2
{
  get { return __someString2?? ""; }
  set { __someString2 = value; OnPropertyChanged(@"_someString2"); }
}
[global::System.Xml.Serialization.XmlIgnore]
[global::System.ComponentModel.Browsable(false)]
public bool _someString2Specified
{
  get { return this.__someString2 != null; }
  set { if (value == (this.__someString2== null)) this.__someString2 = value ? this._someString2 : (string)null; }
}
private bool ShouldSerialize_someString2() { return _someString2Specified; }
private void Reset_someString2() { _someString2Specified = false; }

private readonly global::System.Collections.Generic.List<MyMessage1> __myMessages1 = new global::System.Collections.Generic.List<MyMessage1>();
[global::ProtoBuf.ProtoMember(6, Name=@"_myMessages1", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<MyMessage1> _myMessages1
{
  get { return __myMessages1; }
}

private readonly global::System.Collections.Generic.List<MyMessage2> __myMessages2 = new global::System.Collections.Generic.List<MyMessage2>();
[global::ProtoBuf.ProtoMember(7, Name=@"_myMessages2", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<MyMessage2> _myMessages2
{
  get { return __myMessages2; }
}

private readonly global::System.Collections.Generic.List<MyMessage3> __myMessages3 = new global::System.Collections.Generic.List<MyMessage3>();
[global::ProtoBuf.ProtoMember(8, Name=@"_myMessages3", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<MyMessage3> _myMessages3
{
  get { return __myMessages3; }
}

public event global::System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
  { if(PropertyChanged != null) PropertyChanged(this, new global::System.ComponentModel.PropertyChangedEventArgs(propertyName)); }

private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
  { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}

Solution

  • The driver doesn't by default serialize properties that have no setter. Evidently that includes the messages on Measurement.

    To override the default behavior and include these properties* you either mark them with the BsonElement attribute or map them with RegisterClassMap. In this case where the classes are auto-generated an attribute would be a bad idea so use the latter:

    BsonClassMap.RegisterClassMap<Measurement>(m =>
    {
        m.MapProperty(x => x.MyMessage1);
        m.MapProperty(x => x.MyMessage2);
        m.MapProperty(x => x.MyMessage3);
    });
    

    More in Serialize Documents with the C# Driver: Opt In

    * When a readonly property is serialized, it value is persisted to the database, but never read back out. This is useful for storing “computed” properties