Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 379
Andrew Avatar asked Apr 15 '15 13:04

Andrew


People also ask

What is the e11000 duplicate key error?

The error E11000 duplicate key error ... { person_name: 'Carl-Fredrik Linder' } is saying that the aggregation query is trying to create a document with person_name: 'Carl-Fredrik Linder' more than once - that is violating the unique index constraint on the person_name in the persons collection.

Why is my MongoDB operation key not working?

This error however indicates that the issue has occurred because the key with which you performed the Mongo operation key already exists with the value specified. This also meant the indexed key in the error is unique key and not the non-inique key or sparsed key. That means only one key with unique value can exist in the given mongo collection.

Why is my Mongo database throwing duplicate error?

For example if value is not specified then its possible mongo assigns the null value, then null value will be assigned to very first record but second record will throw the duplicate error issue. Please make sure the indexed fields are properly indexed.

How many unique keys can be added to a Mongo collection?

That means only one key with unique value can exist in the given mongo collection. Generally, you will have default _id as a unique key in the mongo collection. But you can add more unique keys depending on your requirements provided you are 100% sure, its value will remain unique for the whole collection and won’t conflict with other ids.


1 Answers

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.

like image 178
Oliver Drotbohm Avatar answered Sep 23 '22 14:09

Oliver Drotbohm