c# - 通用列表未插入到 MongoDB 中

标签 c# .net mongodb protobuf-net mongodb-.net-driver

我正在尝试将 protobuf-net 对象插入到我的 MongoDB 中。该对象看起来像这样:

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 是同一原始文件中的消息,它们包含一些字符串、 double 和 int64。 我用一些数据填充 Measurement 的实例,并尝试将其插入到我的数据库中,如下所示:

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

现在,如果我检查数据库的内容,我可以看到 instanceOfMeasurement 已正确添加。但其中的重复字段却没有。在将其填充到 MongoDB 之前,我是否需要以某种方式准备一个更复杂的对象?

好的,这是 protobuf-net 创建的内容:

[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); }
}

最佳答案

默认情况下,驱动程序不会序列化没有 setter 的属性。显然,其中包括有关测量的消息。

要覆盖默认行为并包含这些属性*,您可以使用 BsonElement 属性标记它们,或者使用 RegisterClassMap 映射它们。在这种情况下,类是自动生成的,属性将是一个坏主意,因此使用后者:

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

更多内容请参见 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

关于c# - 通用列表未插入到 MongoDB 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25912478/

相关文章:

c# - 如何在我的 WPF 应用程序中使用标准 Windows 警告/错误图标?

C# 将字符串拆分为数组,然后拆分为字典中的键值对

c# - 如何将 YAML 转换为 JSON?

mongodb - 当解析 MongoDB 对象(使用 Mongoose)时,为什么我无法获取 item 元素?

java - 用户搜索功能使用 Java 驱动程序查询 MongoDB 中的数据

c# - Minimod Pretty Print - 如何自定义换行符?

c# - Twincat 3 事件记录器 c# 引用

c# - Application.CurrentCulture 和 Thread.CurrentCulture 之间的区别

c# - 如何在不违反继承安全规则的情况下在 .NET 4+ 中实现 ISerializable?

node.js - 在 Mongoose 中保存对象后如何获取 objectID?