title: MongoDB - Inserting a document with $type causes exception
body: <p>MongoDB is throwing an error when I try and insert the following document:</p>
  
  <pre><code>{
    "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"
  }
  </code></pre>
  
  <p>The error looks like this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The code looks like this:</p>
  
  <pre><code>BsonDocument document = MongoDB.Bson.Serialization.BsonSerializer.Deserialize&lt;BsonDocument&gt;(rawJSON);
  collection.Insert(document);
  </code></pre>
  
  <p>However, if I transpose the "$type" and "$binary" elements of the "_id" element within the "json" element then Mongo is happy for the document to be inserted.  So, this works:</p>
  
  <pre><code>{
    "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"
  }
  </code></pre>
  
  <p>The JSON document is being created by a system slightly outside my control, so getting Mongo to accept the first version is a lot more palatable that having to try to change the format of the JSON.</p>
  
  <p>Can anyone shed any light on why the first version fails but the second works, and how to get the second to work?</p>
  
  <p>I am using the official MongoDB C# driver (v1.8.3.9).</p>
  
  <p>Thanks very much</p>