c# - How to query sub document in mongodb -
i having next collection contains list of programme , each programme containing list of sessions:
{ "_id" : objectid("543f6fd8a4490a19b42c84eb"), "name" : "program1", "tags" : "tag1,tag2", "sessions" : [{ "_id" : objectid("544a00716c6d791820c2d1ae"), "name" : "session1", "tags" : "tag1,tag2" },{ "_id" : objectid("544e426dbb63bc0d94d7ad81"), "name" : "session2", "tags" : "tag1,tag2" }] }, { "_id" : objectid("544e42a1bb63bc0d94d7ad82"), "name" : "program2", "tags" : "tag1,tag2", "sessions" : [{ "_id" : objectid("543f6fd8a4490a19b42c84eb"), "name" : "session1", "tags" : "tag1,tag2" },{ "_id" : objectid("544e4cb1bb63bc0d24333b04"), "name" : "session2", "tags" : "tag1,tag2" }] }
now querying programme following:
var programme = _db.getcollection<program>("program"); imongoquery _query = query<program>.where(e => e.name.contains("program")); programcursor = mongocursor program.find(_query).setsortorder(sortby.ascending("name")).setlimit(itemsperpage).setskip(itemsperpage * (pageno - 1));
now if want query sessions sub document. assuming i've programme id , session name, how query sub document in mongodb c#. didn't found much help on scenario that's why posting so.
you can use
imongoquery _query1 = query<program>.elemmatch(e => e.sessions, builder => builder.matches(session => session.name, "/.*" + search_phrase + ".*/"));
or
imongoquery _query2 = query<program>.elemmatch(e => e.sessions, builder => builder.where(session => session.name.contains(search_phrase)));
and can and , or queries
var query = query.or(new bindinglist<imongoquery> { _query, _query1, .... });
var result = program.find(query);
c# mongodb
No comments:
Post a Comment