BSonValue to boxed value (object) (C# driver)

1,948 views
Skip to first unread message

John Wood (maverin)

unread,
May 30, 2012, 10:25:33 AM5/30/12
to mongod...@googlegroups.com
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?

Robert Stam

unread,
May 30, 2012, 10:30:25 AM5/30/12
to mongod...@googlegroups.com
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) <john...@priorganize.com> 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
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

John Wood (maverin)

unread,
May 30, 2012, 10:36:13 AM5/30/12
to mongod...@googlegroups.com
I need to generically convert a BsonDocument into a Dictionary<string,object>... 

So I can iterate through the BsonDocument as a series of BsonElements, and can get the name as a string (Name), but how can I then just get the value as an object? It seems I would need a large switch statement for each possible type, get the value as the unboxed value, and then cast it to an object to box it?

Any other way to do that?


On Wednesday, May 30, 2012 10:30:25 AM UTC-4, Robert Stam wrote:
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

John Wood (maverin)

unread,
May 30, 2012, 10:38:47 AM5/30/12
to mongod...@googlegroups.com
Actually it wouldn't even be a large switch statement - because I can't see how to get the type of the BsonValue, without running IsXXX for each type? Am I missing something?

Robert Stam

unread,
May 30, 2012, 10:39:37 AM5/30/12
to mongod...@googlegroups.com
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);

Robert Stam

unread,
May 30, 2012, 10:44:13 AM5/30/12
to mongod...@googlegroups.com
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>.

John Wood (maverin)

unread,
May 30, 2012, 10:52:10 AM5/30/12
to mongod...@googlegroups.com
So just to be clear - would the dictionary<string, object> contain:

Key="X" (string)
Value=1 (object)

Key="Y" (string)
Value=2 (object)

After that call? Out of interest, how does the MapToDotNetValue know to convert it specifically to a dictionary<string,object>, or is there an explicit cast  operator defined somewhere to do that?


On Wednesday, May 30, 2012 10:44:13 AM UTC-4, Robert Stam wrote:
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);

Robert Stam

unread,
May 30, 2012, 11:02:51 AM5/30/12
to mongod...@googlegroups.com
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).

John Wood (maverin)

unread,
May 30, 2012, 11:06:09 AM5/30/12
to mongod...@googlegroups.com
Yes I meant Int32 sorry.
Thanks for the explanation though, looks like it will work.


On Wednesday, May 30, 2012 11:02:51 AM UTC-4, Robert Stam wrote:
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).

Reply all
Reply to author
Forward
0 new messages