title: Spring Mongo update by only not null fields of new POJO
body: <p><strong>UserModel:</strong></p>
  
  <pre><code>@Document(collection = "USER")
  public class UserModel {
      private String id;
      private String username;
      @Indexed(unique = true)
      private String email;
      private String password;
      private String firstName;
      private String lastName;
  }
  </code></pre>
  
  <p><strong>UserRepository:</strong></p>
  
  <pre><code>@Repository
  public interface UserRepository extends MongoRepository&lt;UserModel, String&gt; {
  
      Optional&lt;UserModel&gt; findByEmail(String email);
      Optional&lt;UserModel&gt; findByUsername(String username);
  }
  </code></pre>
  
  <p><strong>Saving user document like this:</strong></p>
  
  <pre><code>@Autowired
  UserRepository userRepo;
  
  public someMethod() {
      UserModel user = new UserModel();
      //Set all fields....
      userRepo.save(user);
  }
  </code></pre>
  
  <p><strong>Trying to update like this:</strong></p>
  
  <pre><code>@Autowired
  UserRepository userRepo;
  
  public someMethod() {
      UserModel user = new UserModel();
      user.setId("A valid Id got from database by last insert")
      user.setFirstName("New first name");
      // Remaining fields are null now
      userRepo.save(user);
  }
  </code></pre>
  
  <p>Here null values will be inserted by MongoRepository. </p>
  
  <p>In there any setup by which I can tell <strong>MongoRepository</strong> to avoid null values while updating?</p>
  
  <p>If we do this by <code>MongoTemplate updateFirst()</code> method we have to add <code>if</code> conditions for all fields of a POJO. What is the best solution in this case?</p>