Trying to compare tags in mongodb string array, with textbox input c# -
okay have mongodb has collection called videos , in videos have field called tags. want compare textbox input tags on videos in collection , return them gridview if tag matches input textbox. when create new video tags field string array possible store more 1 tag. trying in c#. hope of can thanks!
code creating new video document.
#region database connection var client = new mongoclient(); var server = client.getserver(); var db = server.getdatabase("database"); #endregion var videos = db.getcollection<video>("videos"); var name = txtvideoname.text; var location = txtvideolocation.text; var description = txtvideodescription.text; var user = txtvideousername.text; string[] lst = txtvideotags.text.split(new char[] { ',' }); var index = videos.count(); var id = 0; if (id <= index) { id += (int)index; } videos.createindex(indexkeys.ascending("tags"), indexoptions.setunique(false)); var newvideo = new video(id, name, location, description, lst, user); videos.insert(newvideo);
okay here how search method looks have made syntax little diffrent grant winney ansewred.
var videos = db.getcollection<video>("videos"); string[] txtinput = txtsearchtags.text.split(new char[] { ',' }); var query = (from x in videos.asqueryable<video>() x.tags.containsany(txtinput) select x);
this finds videos tags contain tag specified in textbox
, assuming mongodb driver can translate valid query.
var videos = db.getcollection<video>("videos") .asqueryable() .where(v => v.tags.split(',') .containsany(txtvideotags.text.split(','))) .tolist();
make sure you've got using mongodb.driver.linq;
@ top.
Comments
Post a Comment