Spring Data Mongo Repository update operation

12,353

Cleaning the Mongo database fixed the problem.

Before I added the @Version Long version, property in my domain class, I had few existing mongo documents in the collection without version property. This was causing the problem.

Share:
12,353
Kumar Sambhav
Author by

Kumar Sambhav

Software developer using Java, Spring(Boot, MVC, Security, Integration, Batch, Reactive), Kotlin, Kafka, RabbitMQ, Zookeeper, MongoDB, PostgreSQL, AWS (EC2, ECS, ELB, Fargate etc.) AngularJS 1.x, Angular 8, Twitter Bootstrap, Angular Material.

Updated on July 21, 2022

Comments

  • Kumar Sambhav
    Kumar Sambhav almost 2 years

    I am using Spring Data Mongo repository for persisting my entities. The parent class of all entities looks like this:-

    @Document
    public abstract class AbstractEntity {
    
        @Id
        private String id;
    
        @CreatedDate
        private Date dateCreated;
    
        @LastModifiedDate
        private Date lastUpdated;
    
        @Version
        private Long version; // This is creating trouble while 'update' operation
     }
    

    This is how I configure Mongo repositorries and auditing:-

    @Configuration
    @EnableMongoRepositories(basePackages = { "x.y.z" })
    @EnableMongoAuditing
    @EnableAutoConfiguration
    public class MongoRepositoryConfig {
    }
    

    I am able to save and 'UPDATE' my entities to Mongo until I don not include the @Version field in my entity for auditing.

    PROBLEM

    If I use the @Version auditing field in my entity class, while trying to update a entity/document using MongoRepository#save(entity) method I am getting following exception:-

    Caused by: com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "localhost:27017" , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.MENU_ITEM.$_id_  dup key: { : ObjectId('541ed581f39d6f87787067e3') }" , "code" : 11000}
        at com.mongodb.CommandResult.getWriteException(CommandResult.java:88)
        at com.mongodb.CommandResult.getException(CommandResult.java:79)
        at com.mongodb.DBCollectionImpl.translateBulkWriteException(DBCollectionImpl.java:314)
        at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:189)
        at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:165)
        at com.mongodb.DBCollection.insert(DBCollection.java:93)
        at com.mongodb.DBCollection.insert(DBCollection.java:78)
        at com.mongodb.DBCollection.insert(DBCollection.java:120)
        at org.springframework.data.mongodb.core.MongoTemplate$8.doInCollection(MongoTemplate.java:900)
        at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:410)
    

    Why upsert operation is failing when the entity has @Version? From what I understand, the version field is used for optimistic locking while update operation.

    The save save methods seems to be trying to do an insert operation instead of update. Is it expected behaviour with @Version?

    <spring.data.mongo.version>1.6.0.RELEASE</spring.data.mongo.version>