插入时出现 BSON 序列化异常。 C#

BSON Serialization Exception on insert. c#

我有这个 BsonDocument,我正在尝试使用 insertOneAsync 插入:

{{
    "starttime": "05.11.2003 17:29:35.189",
    "clk": "0.01",
    "frames": "000001328",
    "typ": "real",
    "MasterRev": "19",
    "WriterVer": "3.0.85",
    "ClientKey": "B4B18CC22F9A4F82FA4D975B53933B5A",
    "Start": "11/05/2003 17:29:35",
    "ID": "File2",
    "InfoCustomEntryA": "AAA0",
    "InfoCustomEntryB": "B1",
    "InfoCustomEntryC": "Just A Standard Entry For The Cold Mill",
    "module_name_0": "RealSignals",
    "module_name_1": "RealSignals5x",
    "module_name_2": "IntegerSignals",
    "module_name_3": "IntegerSignals6x",
    "ibaFilesTest": "12/4/2015 11:46:10 AM",
    "ibaFiles": "6.3.2 Lite (update)",
    "$DATCOOR_status": "processed",
    "$DATCOOR_OutputFiles": "",
    "$DATCOOR_times_tried": "1",
    "$DATCOOR_TasksDone": ""
}
}

在 MongoDB.Driver.Core.dll

中获得 MongoDB.Bson.BsonSerializationException

我该如何调试它?

我序列化时不得不忽略元数据。以下作品。

{{
    "starttime": "05.11.2003 17:29:35.189",
    "clk": "0.01",
    "frames": "000001328",
    "typ": "real",
    "MasterRev": "19",
    "WriterVer": "3.0.85",
    "ClientKey": "B4B18CC22F9A4F82FA4D975B53933B5A",
    "Start": "11/05/2003 17:29:35",
    "ID": "File2",
    "InfoCustomEntryA": "AAA0",
    "InfoCustomEntryB": "B1",
    "InfoCustomEntryC": "Just A Standard Entry For The Cold Mill",
    "module_name_0": "RealSignals",
    "module_name_1": "RealSignals5x",
    "module_name_2": "IntegerSignals",
    "module_name_3": "IntegerSignals6x",
    "ibaFilesTest": "12/4/2015 11:46:10 AM",
    "ibaFiles": "6.3.2 Lite (update)"
}
}

您需要删除多余的大括号才能获得有效 JSON,但是,更重要的是 Mongo 不允许以 $ 开头的字段名称,请参阅此 JIRA 票证.

How can I debug this?

尝试在您对 InsertOneAsync 的调用周围放置一个 try-catch 块,如下所示:

try
{
    YourMongoCollectionHere.InsertOneAsync(YourBsonDocumentHere);
}
catch (MongoException e)
{
    // examine your exception 'e' here
}