Spring Boot Mongo - E11000重复密钥

And*_*rew 4 java spring mongodb spring-mongo spring-data-mongodb

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

Spring的入门指南有一个非常简单的配置,我遵循,但我必须遗漏一些东西.

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

游戏

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

}
Run Code Online (Sandbox Code Playgroud)

游戏仓库

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

}
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

谢谢你的期待!

您可以在此处查看整个项目.

Oli*_*ohm 13

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

诀窍是只使用包装器类型,因为这可能是null,MongoDB检测到缺少值并将ObjectID为您自动填充.但是,由于s不适合数字空间,Long 因此无法正常工作.自动生成支持的ID类型是,和.ObjectIDLongObjectIdStringBigInteger

所有这些都记录在参考文档中.