BsonDocument to IDictionary<string.object>

1,074 views
Skip to first unread message

Chris

unread,
Aug 29, 2011, 12:07:08 PM8/29/11
to mongodb-user
hi guys,

BsonDocument has nested documents and I have to represent this
structure as dictionary of dictionaries.
How to convert BsonDocument to IDictionary<string,object> like
Document.ToDictionary() from samus driver does ?

Robert Stam

unread,
Aug 29, 2011, 2:32:46 PM8/29/11
to mongodb-user
There isn't an equivalent method in the official C# driver. You could
create a JIRA ticket for this if you would like to suggest adding such
a method.

I took a quick look at the method in the samus driver, and I'm not
sure it would always do what you think it does. In particular, it only
seems to convert the top level of the Document to a Dictionary, so any
nested documents would themselves still be of type Document and not of
type Dictionary. Probably not what you were expecting..

If all you want is to convert the top level to a Dictionary, you could
write something like this:

var dictionary = new Dictionary<string, object>();
foreach (var e in document) { dictionary.Add(e.Name, e.Value); }

To recursively convert a nested document structure to a dictionary of
dictionaries would take some helper functions.

Chris

unread,
Aug 29, 2011, 4:56:08 PM8/29/11
to mongodb-user
I can see that official 10gen driver accepts IDictionary as
constructor parameter so I think it's reasonable to except it to
return IDictionary as well ?

I got another question. I've been using samus driver from the
beginning of my trip with mongo. Thanks to it I learnt mongo and
develop reporting/analytic app.
I spent some time of testing and found that the driver doesn't support
replica set: it doesn't take into consideration isMaster.
I need that feature pretty badly and started to looking at other
drivers:
the official 10gen require too many changes (it's actually deadline),
however It's going to be final choice
I found lanwin / mongodb-csharp but cannot find wiki/documentation on
it
So my questions:
is it production ready ?
what's the difference between the samus on ald lanwin one ?
does it support replica set ?

thank you in advance

Robert Stam

unread,
Aug 29, 2011, 5:08:57 PM8/29/11
to mongodb-user
You are right, adding a ToDictionary does have a certain symmetry to
it. I've created a JIRA ticket for it:

https://jira.mongodb.org/browse/CSHARP-312

I can't really give you any advice on the samus driver and the
differences between the various forks of it.

Supporting replica sets is hard. The official driver supports replica
sets but even the latest version still has some bugs in how it handles
failover (which can be worked around by calling Reconnect, but calling
Reconnect too often can also cause other problems).
Reply all
Reply to author
Forward
0 new messages