Spring Boot Mongo - E11000重复键

5
我正在使用spring-boot-starter-data-mongodb构建一个简单的REST api,但是在尝试插入第二行时,总是会出现E11000重复键错误。
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}

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

}

游戏仓库

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

}

应用程序配置

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

}

感谢查看!

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

2个回答

18

您正在使用基本类型long,它具有隐式的预分配值。因此,该值被传递到MongoDB,并将其保留为原样,因为它假定您想手动定义标识符。

关键是只需使用包装器类型,因为它可以是null,MongoDB检测到值的缺失并将自动为您填充ObjectID。但是,Long 无法工作,因为ObjectID不适合Long的数字空间。支持自动生成的id类型为ObjectIdStringBigInteger

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


1
那就是诀窍!我把我的id字段改成了字符串,问题得到了解决。谢谢! - A. Weatherwax
我在我的POJO中甚至没有一个ID,而且遇到了同样的问题。一旦我将我的ID设置为BigInteger,这个问题就被解决了。谢谢Oliver! - Krishna

0

我还发现,如果您尝试多次插入相同的对象,则会引发类似的错误。显然,这不是因为对象的值相同,而是因为实例的对象引用相同。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接