Retrieve data from mongodb using C# driver -


i'm using official mongodb driver c# in test project , i've insert document c# web application mongodb. in mongo console, db.blog.find() can display entries i've inserted. when tried retrieve them, .net throw exception

"system.invalidoperationexception: readstring can called when currentbsontype string, not when currentbsontype objectid."

my entity class simple

namespace mongodbtest {     public class blog     {         public string _id         {             get;             set;         }          public string title         {             get;             set;         }     } } 

and retrieve code

public list<blog> list() {     mongocollection collection = md.getcollection<blog>("blog");     mongocursor<blog> cursor = collection.findallas<blog>();     cursor.setlimit(5);     return cursor.tolist(); } 

can me out? thanks!

i suppose need mark blog id bsonid (and insert id yourself) attribute:

public class blog {     [bsonid]     public string id {get;set;}      public string title{get;set;} } 

and should okay. issue because not marked field mongodb _id , driver generated _id field type objectid. , when driver trying deserialize can't convert objectid string.

complete example:

mongocollection collection = md.getcollection<blog>("blog"); var blog = new blog(){id = objectid.generatenewid().tostring(),                        title = "first blog"}; collection .insert(blog);  mongocursor<blog> cursor = collection.findallas<blog>(); cursor.setlimit(5);  var list = cursor.tolist(); 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -