Is there a way to get a boxed value (as an object) from a BSonValue? I see there are many methods to get the unboxed value, but how do I just get a boxed value or do I have to just box it myself?
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
Not sure exactly what you mean, but guessing that you mean something like this:BsonValue v = new BsonInt32(1);object obj = (object)v.AsInt32;
In some cases the cast to object isn't required because the compiler will autobox for you.
On Wed, May 30, 2012 at 10:25 AM, John Wood (maverin) wrote:
Is there a way to get a boxed value (as an object) from a BSonValue? I see there are many methods to get the unboxed value, but how do I just get a boxed value or do I have to just box it myself?
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
By the way, when you call MapToDotNetValue you aren't boxing the BsonDocument, you are actually converting from BsonDocument to a newly created Dictionary<string, object>.
On Wed, May 30, 2012 at 10:39 AM, Robert Stam wrote:
Wow, my guess wasn't even close... :)You could use the BsonTypeMapper, like in this example:
BsonDocument document = new BsonDocument{{ "x", 1 },{ "y", 2 }};Dictionary<string, object> dictionary = (Dictionary<string, object>)BsonTypeMapper.MapToDotNetValue(document);
In this example, the values will be instances of System.Int32, not object. While the values of Dictionary<string, object> are declared as type object, the actual instances will be some subclass of object.MapToDotNetValue maps all BsonDocument object model values to the closest equivalent available in the .NET Framework. The closest .NET equivalent for BsonDocument is Dictionary<string, object>. You can have a little bit of control over exactly what BsonDocument maps to if you need to, but Dictionary<string, object> is the default.
In cases where there is no .NET equivalent the value will be left unmapped (that would be BSON types JavaScript, JavaScriptWithScope, MaxKey, MinKey, RegularExpression, Symbol, Timestamp and undefined).