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);
但是,如果我轉置「_id」元素中的「$ type」和「$ binary」元素e「json」元素,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文件,因此讓蒙戈接受的第一個版本是一個很大的美味是不得不嘗試改變JSON格式。
任何人都可以闡明爲什麼第一個版本失敗,但第二個作品,以及如何讓第二個工作?
我使用官方的MongoDB C#驅動程序(v1.8.3.9)。
非常感謝
是否有一個原因,你需要一個名爲'$ type'的字段?我猜想它不喜歡它,因爲有一個關鍵字[$ type](http://docs.mongodb.org/manual/reference/operator/query/type/),沒有關鍵字'$ binary'。所以我猜想,在內部,它首先會掃描關鍵字,並忽略稍後對象字段的關鍵字。 – Shaded
如[文檔](http://docs.mongodb.org/manual/core/document/)所示,「字段名稱不能以美元符號($)字符開頭。」但問題很好,如何它工作嗎? – kranteg
你確定它正在工作嗎?我在我的mongoshell中完成了相同的測試,並且兩者都有相同的錯誤。也許是一個驅動程序的錯誤...你能夠在你的文檔上做一個findOne嗎? – kranteg