Spring Boot Mongo - E11000重复密钥

时间:2015-04-15 13:09:06

标签: java spring mongodb spring-data-mongodb spring-mongo

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

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

我已经删除了该集合,并开始新鲜,第一个文档保存正常,但第二个文件也尝试保存为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);

}
&#13;
&#13;
&#13;

的AppConfig

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

}
&#13;
&#13;
&#13;

感谢您的期待!

You can view the whole project here.

1 个答案:

答案 0 :(得分:10)

您使用的原语long具有隐含的预先指定的值。因此,该值将传递给MongoDB,并且它会保持原样,因为它假设您希望手动定义标识符。

诀窍是只使用包装器类型,因为它可以是null,MongoDB会检测到缺少值并为您自动填充ObjectID。但是,Long 无效,因为ObjectID不适合Long的数字空间。自动生成支持的ID类型为ObjectIdStringBigInteger

所有这些都记录在reference documentation