javaspringmongodbspring-data-mongodbspring-mongo

Spring Boot Mongo - E11000 Duplicate Key


I'm building a simple REST api with spring-boot-starter-data-mongodb and I always get a E11000 duplicate key error when attempting to insert my 2nd row.

Spring's getting started guide has a pretty simple configuration that I followed, but I must be missing something.

I've dropped the collection, and started fresh, the 1st document saves fine, but the second one tries to save as id=0 as well. How do I get Spring/Mongo to increment properly?

Here's the error I'm getting:

org.springframework.dao.DuplicateKeyException: { "serverUsed" : "localhost:27017" , "ok" : 1 , "n" : 0 , "err" : "E11000 duplicate key error index: test.game.$_id_ dup key: { : 0 }" , "code" : 11000}; nested exception is com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "localhost:27017" , "ok" : 1 , "n" : 0 , "err" : "E11000 duplicate key error index: test.game.$_id_ dup key: { : 0 }" , "code" : 11000}

Game

package com.recursivechaos.boredgames.domain;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class Game {

    @Id
    private long id;

    private String title;
    private String description;

    public Game() {
    }

    public long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

Game Repository

package com.recursivechaos.boredgames.repository;

import com.recursivechaos.boredgames.domain.Game;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface GameRepository extends MongoRepository<Game, Long> {

    List<Game> findByTitle(@Param("title") String title);

}

AppConfig

package com.recursivechaos.boredgames.configuration;

import com.mongodb.Mongo;
import org.springframework.context.annotation.Bean;
import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

public class AppConfig {

    public
    @Bean
    MongoDbFactory mongoDbFactory() throws Exception {
        UserCredentials userCredentials = new UserCredentials("username", "password");
        SimpleMongoDbFactory boredgamesdb = new SimpleMongoDbFactory(new Mongo(), "boredgamesdb", userCredentials);
        return boredgamesdb;
    }

    public
    @Bean
    MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
    }

}

Thanks for looking!

You can view the whole project here.


Solution

  • You'r using a primitive long which has an implicit, pre-assigned value. Hence, that value is passed to MongoDB and it persists it as is as it assumes you'd like to define identifiers manually.

    The trick is to just use a wrapper type, as this can be null, MongoDB detects the absence of the value and will auto-populate an ObjectID for you. However, Long is not going to work as ObjectIDs do not fit into the number space of a Long. The id types supported for auto-generation are ObjectId, String and BigInteger.

    All of this is documented in the reference documentation.