How to perform Update or Delete in list using Mongo DB Driver












1















I was learning Mongo DB and in that i have following doubt. Please help



I have a class like following example



public class NoteUser
{
private int userid{get;set;}
private List<notes> notes{get;set;}
}


And now i have a context class to read from the database. And i also have one repository class to read the values from this context class, and this class have the functions like following



private bool DeleteNoteByUserId(int userId, int noteId)
{
//Here i need to delete the note which having id as noteId for user
//who having the id as userId
}

private bool UpdateNoteByUserId(int userId, int noteId, Note objNote)
{
//Here i need to perform update the note with objNote which having id as
//noteId for user who having the id as userId
}


How do i code it using MongoDBDriver in my repository class?










share|improve this question



























    1















    I was learning Mongo DB and in that i have following doubt. Please help



    I have a class like following example



    public class NoteUser
    {
    private int userid{get;set;}
    private List<notes> notes{get;set;}
    }


    And now i have a context class to read from the database. And i also have one repository class to read the values from this context class, and this class have the functions like following



    private bool DeleteNoteByUserId(int userId, int noteId)
    {
    //Here i need to delete the note which having id as noteId for user
    //who having the id as userId
    }

    private bool UpdateNoteByUserId(int userId, int noteId, Note objNote)
    {
    //Here i need to perform update the note with objNote which having id as
    //noteId for user who having the id as userId
    }


    How do i code it using MongoDBDriver in my repository class?










    share|improve this question

























      1












      1








      1








      I was learning Mongo DB and in that i have following doubt. Please help



      I have a class like following example



      public class NoteUser
      {
      private int userid{get;set;}
      private List<notes> notes{get;set;}
      }


      And now i have a context class to read from the database. And i also have one repository class to read the values from this context class, and this class have the functions like following



      private bool DeleteNoteByUserId(int userId, int noteId)
      {
      //Here i need to delete the note which having id as noteId for user
      //who having the id as userId
      }

      private bool UpdateNoteByUserId(int userId, int noteId, Note objNote)
      {
      //Here i need to perform update the note with objNote which having id as
      //noteId for user who having the id as userId
      }


      How do i code it using MongoDBDriver in my repository class?










      share|improve this question














      I was learning Mongo DB and in that i have following doubt. Please help



      I have a class like following example



      public class NoteUser
      {
      private int userid{get;set;}
      private List<notes> notes{get;set;}
      }


      And now i have a context class to read from the database. And i also have one repository class to read the values from this context class, and this class have the functions like following



      private bool DeleteNoteByUserId(int userId, int noteId)
      {
      //Here i need to delete the note which having id as noteId for user
      //who having the id as userId
      }

      private bool UpdateNoteByUserId(int userId, int noteId, Note objNote)
      {
      //Here i need to perform update the note with objNote which having id as
      //noteId for user who having the id as userId
      }


      How do i code it using MongoDBDriver in my repository class?







      c# mongodb mongodb-query mongodb-.net-driver






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 20:36









      Nithin PaulNithin Paul

      1,03311937




      1,03311937
























          1 Answer
          1






          active

          oldest

          votes


















          1














          To delete a note (nested object) you have to use $pull operator and specify filtering condition. In C# you can use following code:



          var filter = Builders<NoteUser>.Filter.Eq(x => x.userid, userId);
          var update = Builders<NoteUser>.Update.PullFilter(f => f.notes, p => p.noteid == noteId);

          Col.UpdateOne(filter, update);


          To update nested object you can use $ positional operator and specify all properties. Value -1 represents the note that is matched by filter part.



          var filter = Builders<NoteUser>.Filter.And(Builders<NoteUser>.Filter.Eq(x => x.userid, userId), 
          Builders<NoteUser>.Filter.ElemMatch(x => x.notes, f => f.noteid == noteId));

          var update = Builders<NoteUser>.Update.Combine(
          Builders<NoteUser>.Update.Set(user => user.notes[-1].firstProp, note.firstProp),
          Builders<NoteUser>.Update.Set(user => user.notes[-1].another, note.another));

          Col.UpdateOne(filter, update);





          share|improve this answer



















          • 1





            Beautiful. Thanks Mick it worked :). Its really really tricky unless you get a good grip in Mongo query

            – Nithin Paul
            Nov 17 '18 at 20:34











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345025%2fhow-to-perform-update-or-delete-in-list-using-mongo-db-driver%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          To delete a note (nested object) you have to use $pull operator and specify filtering condition. In C# you can use following code:



          var filter = Builders<NoteUser>.Filter.Eq(x => x.userid, userId);
          var update = Builders<NoteUser>.Update.PullFilter(f => f.notes, p => p.noteid == noteId);

          Col.UpdateOne(filter, update);


          To update nested object you can use $ positional operator and specify all properties. Value -1 represents the note that is matched by filter part.



          var filter = Builders<NoteUser>.Filter.And(Builders<NoteUser>.Filter.Eq(x => x.userid, userId), 
          Builders<NoteUser>.Filter.ElemMatch(x => x.notes, f => f.noteid == noteId));

          var update = Builders<NoteUser>.Update.Combine(
          Builders<NoteUser>.Update.Set(user => user.notes[-1].firstProp, note.firstProp),
          Builders<NoteUser>.Update.Set(user => user.notes[-1].another, note.another));

          Col.UpdateOne(filter, update);





          share|improve this answer



















          • 1





            Beautiful. Thanks Mick it worked :). Its really really tricky unless you get a good grip in Mongo query

            – Nithin Paul
            Nov 17 '18 at 20:34
















          1














          To delete a note (nested object) you have to use $pull operator and specify filtering condition. In C# you can use following code:



          var filter = Builders<NoteUser>.Filter.Eq(x => x.userid, userId);
          var update = Builders<NoteUser>.Update.PullFilter(f => f.notes, p => p.noteid == noteId);

          Col.UpdateOne(filter, update);


          To update nested object you can use $ positional operator and specify all properties. Value -1 represents the note that is matched by filter part.



          var filter = Builders<NoteUser>.Filter.And(Builders<NoteUser>.Filter.Eq(x => x.userid, userId), 
          Builders<NoteUser>.Filter.ElemMatch(x => x.notes, f => f.noteid == noteId));

          var update = Builders<NoteUser>.Update.Combine(
          Builders<NoteUser>.Update.Set(user => user.notes[-1].firstProp, note.firstProp),
          Builders<NoteUser>.Update.Set(user => user.notes[-1].another, note.another));

          Col.UpdateOne(filter, update);





          share|improve this answer



















          • 1





            Beautiful. Thanks Mick it worked :). Its really really tricky unless you get a good grip in Mongo query

            – Nithin Paul
            Nov 17 '18 at 20:34














          1












          1








          1







          To delete a note (nested object) you have to use $pull operator and specify filtering condition. In C# you can use following code:



          var filter = Builders<NoteUser>.Filter.Eq(x => x.userid, userId);
          var update = Builders<NoteUser>.Update.PullFilter(f => f.notes, p => p.noteid == noteId);

          Col.UpdateOne(filter, update);


          To update nested object you can use $ positional operator and specify all properties. Value -1 represents the note that is matched by filter part.



          var filter = Builders<NoteUser>.Filter.And(Builders<NoteUser>.Filter.Eq(x => x.userid, userId), 
          Builders<NoteUser>.Filter.ElemMatch(x => x.notes, f => f.noteid == noteId));

          var update = Builders<NoteUser>.Update.Combine(
          Builders<NoteUser>.Update.Set(user => user.notes[-1].firstProp, note.firstProp),
          Builders<NoteUser>.Update.Set(user => user.notes[-1].another, note.another));

          Col.UpdateOne(filter, update);





          share|improve this answer













          To delete a note (nested object) you have to use $pull operator and specify filtering condition. In C# you can use following code:



          var filter = Builders<NoteUser>.Filter.Eq(x => x.userid, userId);
          var update = Builders<NoteUser>.Update.PullFilter(f => f.notes, p => p.noteid == noteId);

          Col.UpdateOne(filter, update);


          To update nested object you can use $ positional operator and specify all properties. Value -1 represents the note that is matched by filter part.



          var filter = Builders<NoteUser>.Filter.And(Builders<NoteUser>.Filter.Eq(x => x.userid, userId), 
          Builders<NoteUser>.Filter.ElemMatch(x => x.notes, f => f.noteid == noteId));

          var update = Builders<NoteUser>.Update.Combine(
          Builders<NoteUser>.Update.Set(user => user.notes[-1].firstProp, note.firstProp),
          Builders<NoteUser>.Update.Set(user => user.notes[-1].another, note.another));

          Col.UpdateOne(filter, update);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 21:49









          micklmickl

          12.9k51536




          12.9k51536








          • 1





            Beautiful. Thanks Mick it worked :). Its really really tricky unless you get a good grip in Mongo query

            – Nithin Paul
            Nov 17 '18 at 20:34














          • 1





            Beautiful. Thanks Mick it worked :). Its really really tricky unless you get a good grip in Mongo query

            – Nithin Paul
            Nov 17 '18 at 20:34








          1




          1





          Beautiful. Thanks Mick it worked :). Its really really tricky unless you get a good grip in Mongo query

          – Nithin Paul
          Nov 17 '18 at 20:34





          Beautiful. Thanks Mick it worked :). Its really really tricky unless you get a good grip in Mongo query

          – Nithin Paul
          Nov 17 '18 at 20:34


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345025%2fhow-to-perform-update-or-delete-in-list-using-mongo-db-driver%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          1995 France bombings