-
Type: Bug
-
Resolution: Done
-
Priority: 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)
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)
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 =
, 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 =
MongoDB.dll!MongoDB.Bson.BsonWriter.CalculateSize(object obj = {object[4]}
) Line 349 + 0x17 bytes C#
MongoDB.dll!MongoDB.Bson.BsonWriter.CalculateSizeObject(object obj =
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 =
) Line 73 + 0xd bytes C#
MongoDB.dll!MongoDB.Protocol.InsertMessage.Write(System.IO.Stream stream =
) Line 56 + 0xb bytes C#
MongoDB.dll!MongoDB.Connections.Connection.SendMessageCore(MongoDB.Protocol.IRequestMessage message =
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 =
) 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 =
) Line 27 + 0xa bytes C#