title: Why cannot I use the operation 'Update" into Spring Data Mongo DB?
body: <p>I use Spring Data MongoDb for saving documents in database.</p>
  <pre class="lang-java prettyprint-override"><code>@Repository
  public interface UserRepository extends MongoRepository&lt;User, ObjectId&gt; {
  
      Optional&lt;User&gt; findByUsername(String userName);
      
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>@Document(collection = &quot;users&quot;)
  public class User  {
  
      @Id
      private ObjectId id;
  
      @Indexed(unique = true)
      private String username;
  
  ...
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>@Service
  public class UserUpdateServiceImpl implements UserUpdateService {
  
      private UserRepository repository;
  
      private UserMapper mapper;
  
      @Autowired
      public UserUpdateServiceImpl(UserRepository repository, UserMapper mapper) {
          this.repository = repository;
          this.mapper = mapper;
      }
  
      @Override
      public void updateDocumentInCollection(UserDto userDto) {
  
          User user = mapper.toEntity(userDto);
  
          repository.save(user);
      }
  }
  </code></pre>
  <blockquote>
  <p>org.springframework.dao.IncorrectResultSizeDataAccessException: Query
  { &quot;$java&quot; : Query: { &quot;username&quot; : &quot;user&quot;}, Fields: {}, Sort: {} }
  returned non unique result.</p>
  </blockquote>
  <p><strong>Update_2</strong></p>
  <p>I used the operation <code>insert</code></p>
  <pre class="lang-java prettyprint-override"><code>/**
       * Inserts the given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use the
       * returned instance for further operations as the save operation might have changed the entity instance completely.
       * Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
       *
       * @param entity must not be {@literal null}.
       * @return the saved entity
       * @since 1.7
       */
      &lt;S extends T&gt; S insert(S entity);
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>    public void updateUser(UserDto userDto) {
  
          User user = mapper.toEntity(userDto);
  
          userRepository.insert(user);
      }
  </code></pre>
  <p>It again cause a mistake.</p>
  <blockquote>
  <p>org.springframework.dao.DuplicateKeyException: E11000 duplicate key
  error collection: mongo-spring.users index: <em>id</em> dup key: { _id:
  ObjectId('5f3b786085c7d32787990955') }; nested exception is
  com.mongodb.MongoWriteException: E11000 duplicate key error
  collection: mongo-spring.users index: <em>id</em> dup key: { _id:
  ObjectId('5f3b786085c7d32787990955') }</p>
  </blockquote>
  <p><strong>Update_3</strong></p>
  <p>I run unit test.</p>
  <pre class="lang-java prettyprint-override"><code>   @Test
      void updateUser() {
  
          UserDto user = readService.getDataOfUserByName(&quot;user&quot;);
  
          user.setEnabled(true);
          user.setAccountNonLocked(true);
  
          List&lt;Role&gt; authorities = user.getAuthorities();
          authorities.add(Role.ADMIN);
          user.setAuthorities(authorities);
  
  
          if(user.getUsername() != null)
              userUpdateService.updateUser(user);
  
      }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/1cAhZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1cAhZ.png" alt="enter image description here" /></a></p>
  <pre class="lang-java prettyprint-override"><code>    @Override
      public void updateUser(UserDto userDto) {
  
          User user = mapper.toEntity(userDto);
  
          userRepository.save(user);
  
      }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/QZfLC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QZfLC.png" alt="enter image description here" /></a></p>
  <ul>
  <li>mapper</li>
  </ul>
  <pre class="lang-java prettyprint-override"><code>public interface CommonMapper&lt;D, E&gt;{
  
      D toDto(E e);
  
      E toEntity(D d);
  
      Iterable&lt;D&gt; toListDto(Iterable&lt;E&gt; entityList);
  
      Iterable&lt;E&gt; toListEntity(Iterable&lt;D&gt; dtoList);
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>@Mapper(componentModel = &quot;spring&quot;)
  public interface UserMapper extends CommonMapper&lt;UserDto, User&gt; {
  }
  </code></pre>
  <p><strong>Now It work.</strong></p>
  <p>when I try to update a record, I get an exception...</p>