title: I am using c# services and controllers with mongodb , facing issue in patch request I can patch using JsonPatchDocument but cannot save to mongo db
body: <p>I am using MongoDB. I can post put and delete also get, can patch using JsonPatchDocument but face issue when trying to update in db.</p>
  <p>Last Methods in UserService and UserController, which I want make for patch using JsonPatchDocument</p>
  <p><strong>This is my appsetting.json file</strong></p>
  <pre><code>{
        &quot;Logging&quot;: {
          &quot;LogLevel&quot;: {
            &quot;Default&quot;: &quot;Information&quot;,
            &quot;Microsoft.AspNetCore&quot;: &quot;Warning&quot;
          }
        },
        &quot;AllowedHosts&quot;: &quot;*&quot;,
        &quot;HealthCareDatabase&quot;: {
          &quot;ConnectionString&quot;: &quot;mongodb://localhost:27017&quot;,
          &quot;DatabaseName&quot;: &quot;HealthApp&quot;,
          &quot;UserCollectionName&quot;:&quot;User&quot;,
          &quot;JobCollectionName&quot;:&quot;Jobs&quot;,
          &quot;JobApplicationCollectionName&quot;:&quot;jobApplications&quot;,
          &quot;EducationCollectionName&quot;:&quot;Education&quot;,
          &quot;ExperienceCollectionName&quot;:&quot;Experience&quot;,
          &quot;TestCollectionName&quot;:&quot;Test&quot;
      }
  }
  </code></pre>
  <p><strong>UserServices.cs</strong></p>
  <pre><code>using backend.Models;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.Extensions.Options;
  using MongoDB.Driver;
  using Microsoft.AspNetCore.JsonPatch;
  
  namespace backend.Services;
  
  public class UserService :ControllerBase
  {
      private readonly IMongoCollection&lt;User&gt; _userCollection;
  
  
      public UserService(
      IOptions&lt;HealthCareDbSettings&gt; HealthCareDbSettings)
      {
          var mongoClient = new MongoClient(
              HealthCareDbSettings.Value.ConnectionString);
  
          var mongoDatabase = mongoClient.GetDatabase(
              HealthCareDbSettings.Value.DatabaseName);
  
          _userCollection = mongoDatabase.GetCollection&lt;User&gt;(
              HealthCareDbSettings.Value.UserCollectionName);
      }
  
      // User Services are down there
  
      public async Task&lt;List&lt;User&gt;&gt; GetAsync() =&gt;
          await _userCollection.Find(_ =&gt; true).ToListAsync();
  
      public async Task&lt;User?&gt; GetAsync(string id) =&gt;
      await _userCollection.Find(user =&gt; user.Id == id).FirstOrDefaultAsync();
      public User Login(string email, string password)
      {
          var user =  _userCollection.Find(user =&gt; user.Email == email ).FirstOrDefault();
          bool isPasswordValid = BCrypt.Net.BCrypt.Verify(password,user.Password);
          if (isPasswordValid)
          {
              return user;
          }
          else{        
              return  null;
          }
           
  
      }
  
      public async Task CreateUser(User newUser) =&gt;
       await _userCollection.InsertOneAsync(newUser);
  
       public async Task UpdateUser(string id,User user) =&gt;
       await _userCollection.ReplaceOneAsync(user =&gt; user.Id == id,user);
  
  
      // This method has error
       public async Task&lt;User&gt; updateUser(string id, JsonPatchDocument&lt;User&gt; patchDoc) {
          var fromDb = await  _userCollection.Find(user =&gt; user.Id == id).FirstOrDefaultAsync();
          var filter = _userCollection.Find(user =&gt; user.Id == id).FirstOrDefaultAsync();
          patchDoc.ApplyTo(fromDb,ModelState);
          _userCollection.UpdateOne(filter,fromDb); 
          var user = await  _userCollection.Find(user =&gt; user.Id == id).FirstOrDefaultAsync();
          return user;
       }
  
  }
  </code></pre>
  <p><strong>UserController.cs</strong></p>
  <pre><code>using Microsoft.AspNetCore.Mvc;
  using backend.Services;
  using backend.Models;
  using Microsoft.AspNetCore.JsonPatch;
  
  namespace backend.Controllers;
  [ApiController]
  [Route(&quot;api/[controller]&quot;)]
  
  
  public class UserController : ControllerBase
  {
      private readonly UserService _userService;
      public UserController(UserService userservice)
      {
          _userService = userservice;
      }
       public string err = &quot;Invalid Credentials&quot;;
  
      [HttpGet]
      public async Task&lt;List&lt;User&gt;&gt; Get() =&gt;
        await _userService.GetAsync();
  
      [HttpGet(&quot;{id:length(24)}&quot;)]
     public async Task&lt;ActionResult&lt;User&gt;&gt; Get(string id)
     {
          var user = await _userService.GetAsync(id);
          if(user is null){
              return NotFound();
          }
          return user;
     }
     
      [HttpPost(&quot;Login&quot;)]
      public  User userLogin([FromBody] Login login)
      {
       
         User usr =   _userService.Login(login.email,login.password);
         if(usr != null){  
          return usr;
         }else {
              
              return null;
         }
   
      }
  
      // Creating New User
      [HttpPost]
      public async Task&lt;IActionResult&gt; Post(User newUser)
      {
          var usr = newUser;
          var hashed = BCrypt.Net.BCrypt.HashPassword(newUser.Password);
          usr.Password = hashed;
  
          await _userService.CreateUser(usr);
          return CreatedAtAction(nameof(Get), new { id = usr.Id }, usr);
      }
  
  
      //Update User 
      //  [HttpPatch(&quot;up/{id:length(24)}&quot;)]
      
      //  public Task&lt;User&gt; update(string id,[FromBody] JsonPatchDocument&lt;User&gt; patchDoc)
      //  {
      //     var usr = _userService.GetAsync(id);
      //    return  _userService.updateUser(id,patchDoc);
      //  }
  }
  </code></pre>