mongodb - Exclude array element in mongo for C# -


using following json

{     "_id" : objectid("4f7ee46e08403d063ab0b4f9"),     "name" : "mongodb",     "notes" : [                 {                   "title" : "hello mongodb",                   "content" : "hello mongodb"                 },                 {                   "title" : "replicaset mongodb",                   "content" : "replicaset mongodb"                 }              ]     } 

i want use c# projecttion exclude array element it's title not "hello mongodb"

yes possible using aggregation framework, if fine please go following mongo query

db.totest.aggregate([     { $unwind : "$notes" },     { "$match" : { "notes.title" : { "$ne" : "hello mongodb" } } },     { $group : { _id : {"name": "$name","_id": "$_id" }, notes: { $push: "$notes" } } } ,     {$project:{_id:0,"name":"$_id.name","_id":"$_id._id","notes":"$notes"}}]) 

for c# using mongodb.driver 1.9.0

public void getdata() {     ilist<bsondocument> pipeline =new list<bsondocument>();     ///unwind query     bsondocument _ounwind = new bsondocument("$unwind", "$notes");     pipeline.add(_ounwind);     ///match query     imongoquery query = query.ne("notes.title", "hello mongodb");     pipeline.add(new bsondocument("$match", query bsondocument));      ///group      ilist<bsonelement> groupbyelement=new list<bsonelement>();     ilist<bsonelement> _id = new list<bsonelement>();     _id.add(new bsonelement("name", "$name"));     _id.add(new bsonelement("_id", "$_id"));     groupbyelement.add(new bsonelement("_id", new bsondocument(_id)));     groupbyelement.add(new bsonelement("notes",new bsondocument("$push","$notes")));     bsondocument _groupby = new bsondocument("$group", new bsondocument(groupbyelement));     pipeline.add(_groupby);      ///project     ilist<bsonelement> projectelement = new list<bsonelement>();     projectelement.add(new bsonelement("_id", 0));     projectelement.add(new bsonelement("name", "$_id.name"));     projectelement.add(new bsonelement("_id", "$_id._id"));     projectelement.add(new bsonelement("notes", "$notes"));     bsondocument _project = new bsondocument("$project", new bsondocument(projectelement));     pipeline.add(_project);      aggregateargs _oaggregateargs=new aggregateargs();     _oaggregateargs.pipeline =pipeline;      ///mongo database     mongodatabase _omongodatabase = mognocontext.getdatabase();      ///final     ilist<bsondocument> result = _omongodatabase.getcollection<totest>("totest").aggregate(_oaggregateargs).tolist();     ilist<totest> finalresult = new list<totest>();     foreach (bsondocument itrbsondocument in result)     {         totest _ototest= bsonserializer.deserialize<totest>(itrbsondocument);         finalresult.add(_ototest);     } } 

Comments

Post a Comment

Popular posts from this blog

python - Error importing VideoFileClip from moviepy : AttributeError: 'PermissionError' object has no attribute 'message' -

java - is not an enclosing class / new Intent Cannot Resolve Constructor -

qt - QML MouseArea onWheel event not working properly when inside QML Scrollview -