Spring 启动 Mongo - E11000 重复密钥

Spring Boot Mongo - E11000 Duplicate Key

我正在使用 spring-boot-starter-data-mongodb 构建一个简单的 REST api,并且在尝试插入第二行时总是得到 E11000 duplicate key error

Spring 的 getting started guide 有一个我遵循的非常简单的配置,但我一定遗漏了一些东西。

我删除了 collection,重新开始,第一个文档保存良好,但第二个文档也尝试保存为 id=0。 如何让 Spring/Mongo 正确递增?

这是我遇到的错误:

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}

游戏

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;
    }

}

游戏库

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());
    }

}

感谢观看!

You can view the whole project here.

您正在使用具有隐式预分配值的基元 long。因此,该值被传递给 MongoDB 并按原样保留它,因为它假设您想要手动定义标识符。

诀窍是只使用包装器类型,因为这可以是 null,MongoDB 检测到值的缺失并会自动为您填充 ObjectID。但是,Long 不会起作用 ,因为 ObjectID 不适合 Long 的数字 space。自动生成支持的id类型有ObjectIdStringBigInteger

所有这些都记录在 reference documentation 中。

我还意识到,如果您多次尝试插入同一个对象,它会抛出类似的错误。显然这不是因为对象的值相同,而是因为对象引用相同,例如

// This would throw the exception    
SomeObject object = new SomeObject()
something.insert(object)
something.insert(object)

// But this will not because the references are different although it is 
// the same object
SomeObject object1 = new SomeObject()
SomeObject object2 = new SomeObject()
something.insert(object)
something.insert(object)