How to convert the text to bsondocument and inverse with C#?

103 views
Skip to first unread message

沉渔涧

unread,
May 21, 2012, 10:58:11 AM5/21/12
to mongod...@googlegroups.com
I'm trying to offer some functions that allow the user to modify the existing bsondocument through the GUI, currently I'm in this phase:

I show the whole hierarchy of the bsondocument in a treeview.

and my questions is :

1. if I  allow user to only to modify the node and its value that he selects, how should I find the correponding bson element in the bsondocument?

2, if I offer some text edting GUI that allow user to modify any part of the bsondocument, and my software should deal with both how bsondocument converted into a well indented text format, and how read the modified text back so that I can serialize it inversely as a updated bsondocument.

craiggwilson

unread,
May 21, 2012, 3:43:37 PM5/21/12
to mongod...@googlegroups.com
1) So, this is fairly unrelated to the driver itself and really more dependent upon the GUI piece and what technology you are using.  If you are using the built-in Windows Forms TreeView control, then there is a Tag property on each node.  You can assign the BsonElement to this Tag property so that when the user edits it, you know exactly what to apply the changes to.  A deletion would mean you'd need to move to the node's parent and remove the element referenced by the subnode's Tag.  e.g. node.Parent.Tag.Remove(node.Tag).  

There are a gillion ways of doing this, and none really relate to the driver itself.

2) You are going to need a JSON parser to get the object into a form you can use. We have a JsonReader that can read a string of json and convert it into a document.  This may take some work and some looking into as I'm unfamiliar with how to accomplish this yet.  I'll try and follow up later with an example.

craiggwilson

unread,
May 22, 2012, 9:55:37 AM5/22/12
to mongod...@googlegroups.com
As for #2: this is accomplished with this code:

  BsonSerializer.Deserialize<BsonDocument>(json);

trypsin

unread,
May 23, 2012, 7:47:38 AM5/23/12
to mongodb-user
@ craiggwilson
So, the process goes as following : general text -> json ->
bsondocument ?

craiggwilson

unread,
May 23, 2012, 6:24:36 PM5/23/12
to mongod...@googlegroups.com
Yes.  But if "general text" is json, then you don't need to parse it first.  

var json = textbox.Text;
var document = BsonSerializer.Deserialize<BsonDocument>(json);
Reply all
Reply to author
Forward
0 new messages