Querying, filtering and updating multiple level nested arrays in MongoDB using C# -


i have mongodb document. developing mvc application , trying update comment array (commented description "comment after update") using c#. i'm using new mongodb version.

{    "projectid":1,    "projectname":"project test",    "projectstatus":"active",    "projecttasks":[       {          "projecttaskid":1,          "taskshortdescription":"short task description",          "tasklongdescription":"long task description",          "comments":[             {                "commentid":1,                "commentdescription":"comment before update",                "createdby":"mike",                "uploaddocuments":{                   "taskid":null,                   "commentid":null,                   "uploaddocumentid":1,                   "uploaddocumentname":"first document upload"                }             }          ]       }    ] } 

i tried using this:

var filter = builders<project>.filter.and(builders<project>.filter.eq(x => x.projectid, projectid), builders<project>.filter.elemmatch(x => x.projecttasks, x => x.projecttaskid == projecttaskid), builders<project>.filter.elemmatch(x => x.projecttasks.elementat(-1).comments, x => x.commentid == comment.commentid));  var update = builders<project>.update.set(x => x.projecttasks.elementat(-1).comments.elementat(-1).commentdescription, comment.commentdescription );  collection.updateoneasync(filter, update, new updateoptions() { isupsert = true }); 

i tried change filter

var filter = builders<project>.filter.and(builders<project>.filter.where(p => p.projectid == projectid), builders<project>.filter.eq("projecttasks.projecttaskid", projecttaskid), builders<project>.filter.eq("projecttasks.$.comments.$.commentid", comment.commentid)); 

for both cases, i'm not able query, filter , update comments.

can please tell me how find , update comments in document? suggestion appreciated!

i faced similar problem - needed insert new item in nested collection. assuming cannot change data structure idea find index of nested entity update. entity becomes accessible in update definition.

in given context did follows:

var projecttaskindex = await _mongocontext.projects     .find(p => p.projectid == projectid)     .project(p => p.projecttasks.findindex(t => t.projecttaskid == projecttaskid))     .singleordefaultasync();  var updatedefinition = new updatedefinitionbuilder<project>()     .addtoset(p => p.projecttasks[projecttaskindex].comments, comment);  await _mongocontext.projects     .updateoneasync(p=> p.projectid == projectid, updatedefinition); 

in same way 1 can find index of comment , access comment's properties. update definition follows:

var updatedefinition = new updatedefinitionbuilder<project>()     .set(p => p.projecttasks[projecttaskindex]                  .comments[commentindex].commentdescription,             comment.commentdescription); 

very important here ensure thread safety - collections of projecttasks , comments must not changes between getting indexes , actual update.

@cisco, have found solution? if so, please share it?


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -