Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-50

Error in MongoDB.Bson.BsonWriter.TranslateToBsonType() when translating Document fields of type System.Byte

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: SAMUS
    • None
    • Environment:
      VS 2008 / .NET 3.5

      The method TranslateToBsonType(object obj) in MongoDB.Bson.BsonWriter does not handle objects of type System.Byte, I am trying to convert a business object to a MongoDB document, the object has fields of type Byte on them, and these are choking. I've given some details below but it really looks like all you should need to fix this is to add this line to the TranslateToBsonType() method among all the other type checks:

      if(type == typeof(Byte))
      return BsonType.Binary;

      My application code:
      var doctable = db.GetCollection("CaseDocuments");
      foreach (FullDocumentInfo doc in docs)

      { doctable.Insert(doc.ToDocument()); //error is thrown deep inside the Insert(). }

      ToDocument() is the extension method described here http://www.highoncoding.com/Articles/680_Implementing_Business_Object_to_Documents_Converter_for_MongoDb.aspx
      I implemented it in a DocConverter class:
      public static class DocConverter
      {
      public static T ToClass<T>(this Document source)

      { if (source == null) throw new ArgumentNullException("Document is null!"); return new JavaScriptSerializer().Deserialize<T>(source.ToString()); }

      public static Document ToDocument(this object source)

      { var document = SerializeMember(source) as Document; return document; }

      private static object SerializeMember(object source)
      {
      // get the properties

      if (!Type.GetTypeCode(source.GetType()).Equals(TypeCode.Object))
      return source;

      // if the object is IEnumerable

      var enumerable = source as IEnumerable;

      if (enumerable != null)
      {
      var documents = new List<Object>();

      foreach (var doc in enumerable)

      { documents.Add(SerializeMember(doc)); }

      return documents.ToArray();
      }

      var properties = source.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);

      var document = new Document();

      foreach (var property in properties)

      { var propertyValue = property.GetValue(source, null); if (propertyValue == null) continue; document.Add(property.Name, SerializeMember(propertyValue)); }

      return document;
      }
      }

      Stack trace:
      > MongoDB.dll!MongoDB.Bson.BsonWriter.TranslateToBsonType(object obj = 46) Line 612 C#
      MongoDB.dll!MongoDB.Bson.BsonWriter.CalculateSize(object obj = 46) Line 325 + 0xb bytes C#
      MongoDB.dll!MongoDB.Bson.BsonWriter.CalculateSizeObject(object obj =

      {MongoDB.Serialization.Descriptors.ArrayDescriptor}

      , System.Collections.Generic.IEnumerable<MongoDB.Bson.BsonProperty> propertys =

      {MongoDB.Serialization.Descriptors.ArrayDescriptor.GetProperties}

      ) Line 485 + 0x24 bytes C#
      MongoDB.dll!MongoDB.Bson.BsonWriter.CalculateSize(System.Collections.IEnumerable enumerable =

      {object[4]}) Line 503 + 0xe bytes C#
      MongoDB.dll!MongoDB.Bson.BsonWriter.CalculateSize(object obj = {object[4]}

      ) Line 349 + 0x17 bytes C#
      MongoDB.dll!MongoDB.Bson.BsonWriter.CalculateSizeObject(object obj =

      {MongoDB.Serialization.Descriptors.DocumentPropertyDescriptor}, System.Collections.Generic.IEnumerable<MongoDB.Bson.BsonProperty> propertys = {System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Collections.Generic.KeyValuePair<string,object>,MongoDB.Bson.BsonProperty>}) Line 485 + 0x24 bytes C#
      MongoDB.dll!MongoDB.Bson.BsonWriter.CalculateSizeObject(object obj = {MongoDB.Serialization.Descriptors.DocumentPropertyDescriptor}

      ) Line 464 + 0xe bytes C#
      MongoDB.dll!MongoDB.Bson.BsonWriter.CalculateSize(object obj) Line 347 + 0x39 bytes C#
      MongoDB.dll!MongoDB.Protocol.InsertMessage.ChunkMessage(MongoDB.Bson.BsonWriter writer =

      {MongoDB.Bson.BsonWriter}

      ) Line 73 + 0xd bytes C#
      MongoDB.dll!MongoDB.Protocol.InsertMessage.Write(System.IO.Stream stream =

      {System.Net.Sockets.NetworkStream}

      ) Line 56 + 0xb bytes C#
      MongoDB.dll!MongoDB.Connections.Connection.SendMessageCore(MongoDB.Protocol.IRequestMessage message =

      {MongoDB.Protocol.InsertMessage}) Line 141 + 0x22 bytes C#
      MongoDB.dll!MongoDB.Connections.Connection.SendMessage(MongoDB.Protocol.IRequestMessage message = {MongoDB.Protocol.InsertMessage}

      , string database = "DocTest1") Line 126 + 0xb bytes C#
      MongoDB.dll!MongoDB.MongoCollection<MongoDB.Document>.Insert<MongoDB.Document>(System.Collections.Generic.IEnumerable<MongoDB.Document> documents =

      {MongoDB.Document[1]}

      ) Line 333 + 0x3c bytes C#
      MongoDB.dll!MongoDB.MongoCollection<MongoDB.Document>.Insert<System.Collections.Generic.KeyValuePair<string,object>>(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object>> documents) Line 306 + 0xac bytes C#
      MongoDB.dll!MongoDB.MongoCollection.Insert(MongoDB.Document document) Line 224 + 0x1b bytes C#
      Simple.exe!Simple.DocTest1.CopyDocData() Line 38 + 0x29 bytes C#
      Simple.exe!Simple.MainClass.Main(string[] args =

      {string[0]}

      ) Line 27 + 0xa bytes C#

            Assignee:
            sam Sam Corder
            Reporter:
            anyeone Anye Mercy
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: