title: How to update duplicate entries in MongoDB with Spring Data
body: <p>I have following entity-model which I'll save to my MongoDB:</p>
  <pre><code>@Document(collection = &quot;googleplaygames&quot;)
  @Component
  public final class GooglePlayGame implements Serializable {
  
      @Id
      private String title;
      private String genre;
      private String price;
      private LocalDate dateOfLastUpdate;
      ...
  </code></pre>
  <p>This code allows me to save duplicates Game objects. I found annotation @Indexed and rewrote code:</p>
  <pre><code>@Document(collection = &quot;googleplaygames&quot;)
  @Component
  public final class GooglePlayGame implements Serializable {
  
      @Indexed(unique=true)
      private String title;
      private String genre;
      private String price;
      private LocalDate dateOfLastUpdate;
      ...
  </code></pre>
  <p>Now if I'll try to save entity with same title, I'll receive <code>org.springframework.dao.DuplicateKeyException</code>. Fair enough.</p>
  <p>And I found this &quot;error&quot; in logs while Spring Boot app is starting:</p>
  <pre><code>2020-06-26 13:26:52,303 WARN  [restartedMain] org.springframework.data.mongodb.core.index.JustOnceLogger: Automatic index creation will be disabled by default as of Spring Data MongoDB 3.x.
      Please use 'MongoMappingContext#setAutoIndexCreation(boolean)' or override 'MongoConfigurationSupport#autoIndexCreation()' to be explicit.
      However, we recommend setting up indices manually in an application ready block. You may use index derivation there as well.
  
      &gt; -----------------------------------------------------------------------------------------
      &gt; @EventListener(ApplicationReadyEvent.class)
      &gt; public void initIndicesAfterStartup() {
      &gt;
      &gt;     IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
      &gt;
      &gt;     IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
      &gt;     resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
      &gt; }
      &gt; -----------------------------------------------------------------------------------------
  </code></pre>
  <p>But I want to update the enity, if any of other fields are different. For example, if I have an entity with title &quot;Dead Cells&quot; and version &quot;1.0&quot; in my db, I want to update this entity if version now is &quot;1.1&quot;. But code above doesn't allow me to do this.</p>
  <p>So what is this error? And how to update entity (doesnt allow duplicate by title field, but allow to rewrite entity, if other fields were changed).</p>