insert json to monodb in .net application

28 views
Skip to first unread message

Maddy

unread,
Dec 29, 2010, 4:14:41 AM12/29/10
to mongodb-user
Hi All,

i am new to MONGO DB please tell me how to insert json string to
monogoDB. i tried by directly giving the TOBSAONDOCUMENT but its
inserting only the object id not able to insert through application.
but whereas i can able to insert the same string in Backend in DB.
Please help me out

Ken Egozi

unread,
Dec 29, 2010, 8:14:50 AM12/29/10
to mongod...@googlegroups.com
you'd need to transform the JSON into BSON.
afaik this functionality is not yet implemented in the csharp driver directly.

however you should be able to get around it
a. use a JSON deserializer to get a Dictionary<string,object> from your JSON.  Newtonsoft's JSON library should do this in a one liner easily.
b. create a new BsonDocument;
c. go through the Dictionary, and add each value (or array, or nested dictionary) into the BsonDocument.




--
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.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.




--
Ken Egozi.
http://www.kenegozi.com/blog
http://www.delver.com
http://www.musicglue.com
http://www.castleproject.org
http://www.idcc.co.il - הכנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם

Robert Stam

unread,
Dec 29, 2010, 9:33:25 AM12/29/10
to mongodb-user
Version 1.0 of the C# driver will have support for reading JSON. You
can try it out now if you get the latest version from github and
compile it yourself.

Here's some sample code:

https://gist.github.com/a745c04f452e3bd0b363

Assuming a string variable called "json" you can convert it to a
BsonDocument using one line of code (and insert it into a collection
with one more):

var document = BsonDocument.ReadFrom(BsonReader.Create(json));
collection.Insert(document);

On Dec 29, 8:14 am, Ken Egozi <egoz...@gmail.com> wrote:
> you'd need to transform the JSON into BSON.
> afaik this functionality is not yet implemented in the csharp driver
> directly.
>
> however you should be able to get around it
> a. use a JSON deserializer to get a Dictionary<string,object> from your
> JSON.  Newtonsoft's JSON library should do this in a one liner easily.
> b. create a new BsonDocument;
> c. go through the Dictionary, and add each value (or array, or nested
> dictionary) into the BsonDocument.
>
>
>
> On Wed, Dec 29, 2010 at 11:14 AM, Maddy <sarvesh....@gmail.com> wrote:
> > Hi All,
>
> > i am new to MONGO DB please tell me how to insert json string to
> > monogoDB. i tried by directly giving the TOBSAONDOCUMENT but its
> > inserting only the object id not able to insert through application.
> > but whereas i can able to insert the same string in Backend in DB.
> > Please help me out
>
> > --
> > 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<mongodb-user%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/mongodb-user?hl=en.
>
> --
> Ken Egozi.http://www.kenegozi.com/bloghttp://www.delver.comhttp://www.musicglue.comhttp://www.castleproject.orghttp://www.idcc.co.il- הכנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם

Ken Egozi

unread,
Dec 29, 2010, 10:06:10 AM12/29/10
to mongod...@googlegroups.com
coooooooooooooooooooooooool

To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.




--
http://www.idcc.co.il - הכנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם
Reply all
Reply to author
Forward
0 new messages