title: Updating compound indexes document in spring data mongo DB
body: <p>I have a mongo db collection which has a compound index set on 2 of its fields as below</p>
  
  <pre><code>@Document(collection = “customer”)
  @Data
  @Builder
  @NoArgsConstructor
  @AllArgsConstructor
  @CompoundIndexes({ @CompoundIndex(name = idx1,unique=true ,def = “{‘customerId’ : 1, ‘managerId’: 1}") })
  public class CustomerInfo implements Serializable{
  private static final long serialVersionUID = -4743404231218445458L;
      private String customerName;
      private Long customerId;
      private Long managerId;
      private Date lastUpdatedTs;
  }
  </code></pre>
  
  <p>Now when I try to update the collection by using MongoRepository.save() method am getting the below exception </p>
  
  <pre><code>Caused by: org.springframework.dao.DuplicateKeyException: Write failed with error code 11000 and error message 'E11000 duplicate key error collection: customerDB.customer index: idx1 dup key: { : 4790, : 17 }'; nested exception is com.mongodb.DuplicateKeyException: Write failed with error code 11000 and error message 'E11000 duplicate key error collection: customerDB.customer index: idx1 dup key: { : 4790, : 17 }'
      at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:73) ~[spring-data-mongodb-1.10.9.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2146) ~[spring-data-mongodb-1.10.9.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:484) ~[spring-data-mongodb-1.10.9.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.MongoTemplate.insertDBObject(MongoTemplate.java:1049) ~[spring-data-mongodb-1.10.9.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:858) ~[spring-data-mongodb-1.10.9.RELEASE.jar:na]
      at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:799) ~[spring-data-mongodb-1.10.9.RELEASE.jar:na]
      at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:80) ~[spring-data-mongodb-1.10.9.RELEASE.jar:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
      at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
  </code></pre>
  
  <p>I know the issue is because of the way the MongoRepository.save() method as it relies on the entityInformation.isNew() to identify if it is a save or update use case .For me it has to rely on the uniqueness of the compound index to identify if it is update or not .For me the compound index is the primary key for the mongo collection .Any ideas would be appreciated !!</p>