MongoDB - 使用$ type插入文档会导致异常

时间:2014-05-13 15:59:34

标签: c# json mongodb

当我尝试插入以下文档时,MongoDB会抛出错误:

{
  "dt": "2014-05-13 02:43:01.454",
  "ev": "coredatasaveform",
  "ci": "rQBDXUAbSqy8BP3ZuOXWVg==",
  "mi": "Hw5ySHcJSO+HKxBD1s4zQA==",
  "on": "Company",
  "json": {
    "_id": {
      "$type": "03",
      "$binary": "O1tFU+smDEGo/v/hB1/giw=="
    },
    "_lastmodifieddatelocalutc": "2014-05-13 02:43:01",
    "name": "A company name here",
    "_lastmodifiedonservertime": {
      "$date": 1398876087000
    },
    "_zv": "1.8",
    "_mi": "Hw5ySHcJSO+HKxBD1s4zQA==",
    "sourceofdata": "xxxccc"
  },
  "na": "HPSDbUtilities persistCapturedModelValuesSetWithModel (null)",
  "rc": "1"
}

错误如下所示:

03:44:02.0421 WriteMongoDocument Exception was :Element name '$type' is not valid because it starts with a '$'.    at MongoDB.Bson.IO.BsonWriter.CheckElementName(String name)
   at MongoDB.Bson.IO.BsonWriter.WriteName(String name)
   at MongoDB.Bson.Serialization.Serializers.BsonDocumentSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
   at MongoDB.Bson.Serialization.Serializers.BsonValueSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
   at MongoDB.Bson.Serialization.Serializers.BsonDocumentSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
   at MongoDB.Bson.Serialization.Serializers.BsonValueSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
   at MongoDB.Bson.Serialization.Serializers.BsonDocumentSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
   at MongoDB.Driver.Internal.MongoInsertMessage.AddDocument(BsonBuffer buffer, Type nominalType, Object document)
   at MongoDB.Driver.Operations.InsertOperation.Execute(MongoConnection connection)
   at MongoDB.Driver.MongoCollection.InsertBatch(Type nominalType, IEnumerable documents, MongoInsertOptions options)
   at MongoDB.Driver.MongoCollection.Insert(Type nominalType, Object document, MongoInsertOptions options)
   at MongoDB.Driver.MongoCollection.Insert(Type nominalType, Object document)
   at MongoDB.Driver.MongoCollection.Insert[TNominalType](TNominalType document)
   at Tradeshow.Models.Mongo.WriteMongoLogDocument(String rawJSON, Int32 ix) in C:\Inetpub\wwwroot\Tradeshow\Tradeshow\Models\Mongo.cs:line 665

代码如下所示:

BsonDocument document = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(rawJSON);
collection.Insert(document);

但是,如果我转换&#34; $ type&#34;和&#34; $ binary&#34; &#34; _id&#34;的元素&#34; json&#34;中的元素元素然后Mongo很高兴要插入文档。所以,这有效:

{
  "dt": "2014-05-13 02:43:01.454",
  "ev": "coredatasaveform",
  "ci": "rQBDXUAbSqy8BP3ZuOXWVg==",
  "mi": "Hw5ySHcJSO+HKxBD1s4zQA==",
  "on": "Company",
  "json": {
    "_id": {
      "$binary": "O1tFU+smDEGo/v/hB1/giw=="
      ,"$type": "03"
    },
    "_lastmodifieddatelocalutc": "2014-05-13 02:43:01",
    "name": "A company name here",
    "_lastmodifiedonservertime": {
      "$date": 1398876087000
    },
    "_zv": "1.8",
    "_mi": "Hw5ySHcJSO+HKxBD1s4zQA==",
    "sourceofdata": "xxxccc"
  },
  "na": "HPSDbUtilities persistCapturedModelValuesSetWithModel (null)",
  "rc": "1"
}

JSON文档是由一个稍微不受我控制的系统创建的,因此让Mongo接受第一个版本更加可口,不得不尝试更改JSON的格式。

任何人都可以解释为什么第一个版本失败但第二个版本失败,以及如何让第二个版本工作?

我正在使用官方的MongoDB C#驱动程序(v1.8.3.9)。

非常感谢

1 个答案:

答案 0 :(得分:1)

这里的问题是,它实际上是MongoDB的extended JSON synta x的一部分,它实际上是在C#驱动程序的JSON Utils中支持的。

如果按正确的顺序显示字段时显示为directly in the documentation

"_id": {
  "$binary": "O1tFU+smDEGo/v/hB1/giw==",
  "$type": "03"
}

然后,实际值将在内部BSON表示中转换为BinData类型的字段。

由于JSON,因此BSON维持键值的顺序,如果它们实际上被反转,它们的显示顺序将导致问题。

因此,您的错误来源。

你可能会看一些这些数据的初步解析,或者找到一种操作块的方法,然后再将其他函数传递给JSON utils函数,直接解析为BSON。

仅供参考,你甚至可以在unit tests中看到驱动源的演示。