c# - How to read a Mongo Document and save it to an object? -
i need read fields in documents mongo. filtered out document using filter.eq() method how can find , store field in document? code:
public void human_radiobutton_checked(object sender, routedeventargs e) { upload human = new upload(); //opens connection database , collection var filter = builders<bsondocument>.filter.eq("name", "human"); race_desc description = new race_desc(); description.desc = convert.tostring(filter); desc_textbox.text = description.desc; however, doesn't work since didn't grab fields, document. how can read field called 'a' , store object?
thanks
when define filter using builders<bsondocument>.filter here, merely define filter can use/execute in query. storing sql string in string variable.
what missed here executing filter , retrieve data. according mongodb c# driver doc:
var collection = _database.getcollection<bsondocument>("restaurants"); var filter = builders<bsondocument>.filter.eq("borough", "manhattan"); var result = await collection.find(filter).tolistasync(); then can iterate results , access properties like
foreach(var result in results) { //do here result.your_property } and if want first of result
var result = (await collection.find(filter).tolistasync()).firstordefault(); now if want cast bsondocument got own class deserialize document using bsonserializer:
var myobj = bsonserializer.deserialize<yourtype>(result); you can map own class:
var yourclass = new yourtype(); yourclass.stuff= result["stuff"].tostring();
Comments
Post a Comment