如何在Spring Boot项目中允许MongoDB的重复键?

huangapple go评论51阅读模式
英文:

How do I allow duplicate key for MongoDB in Spring Boot Project?

问题

这是我的文档的外观:

@Getter
@Document(collection = "team")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Team extends BaseTimeEntity {
    ....
    private List<User> designers = new ArrayList<>();
    ....
}

继承自这个类的BaseTimeEntity看起来像这样:

@Getter
public abstract class BaseTimeEntity {

    @MongoId
    private ObjectId id;

    @CreatedDate
    @Field(name = "created_date")
    private LocalDateTime createdDate;

    @LastModifiedDate
    @Field(name = "modified_date")
    private LocalDateTime modifiedDate;

    @Field(name = "is_deleted")
    protected Boolean isDeleted;

    @Field(name = "schema_version")
    protected String schemaVersion = "1.0";
}

在将单个Team数据添加到数据库后,当我尝试使用这个类创建新数据,再次将designers列表留空时,我遇到了org.springframework.dao.DuplicateKeyException

这是我收到的错误消息:

org.springframework.dao.DuplicateKeyException: 服务器localhost:27017上的写入操作错误写入错误WriteError{code=11000, message='E11000重复键错误集合gabojait.team索引designer.username重复键{designer.usernamenull}', details={}}嵌套异常是com.mongodb.MongoWriteException服务器localhost:27017上的写入操作错误写入错误WriteError{code=11000, message='E11000重复键错误集合gabojait.team索引designer.username重复键{designer.usernamenull}', details={}}

如何解决这个问题?

英文:

This is how my document looks like

@Getter
@Document(collection = &quot;team&quot;)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Team extends BaseTimeEntity {
    ....
    private List&lt;User&gt; designers = new ArrayList&lt;&gt;();
    ....
}

The BaseTimeEntity, which is extending from this class, looks like this.

@Getter
public abstract class BaseTimeEntity {

    @MongoId
    private ObjectId id;

    @CreatedDate
    @Field(name = &quot;created_date&quot;)
    private LocalDateTime createdDate;

    @LastModifiedDate
    @Field(name = &quot;modified_date&quot;)
    private LocalDateTime modifiedDate;

    @Field(name = &quot;is_deleted&quot;)
    protected Boolean isDeleted;

    @Field(name = &quot;schema_version&quot;)
    protected String schemaVersion = &quot;1.0&quot;;
}

After adding a single Team data into the database. When I try to create a new data with this class, leaving the designers list blank again, I get org.springframework.dao.DuplicateKeyException.

This is the error msg that I get.

org.springframework.dao.DuplicateKeyException: Write operation error on server 
localhost:27017. Write error: WriteError{code=11000, message=&#39;E11000 duplicate key error 
collection: gabojait.team index: designer.username dup key: { designer.username: null }&#39;, 
details={}}.; nested exception is com.mongodb.MongoWriteException: Write operation error 
on server localhost:27017. Write error: WriteError{code=11000, message=&#39;E11000 duplicate 
key error collection: gabojait.team index: designer.username dup key: { 
designer.username: null }&#39;, details={}}.

How do I fix this problem?

答案1

得分: 0

你的集合在设计师用户名字段上定义了一个索引。移除它或为用户名提供唯一值,或在索引上允许非唯一选项。

英文:

your collection has an index defined on the designer.username field. remove it or provide a unique value to the username, or allow non-unique option on the index.

huangapple
  • 本文由 发表于 2023年2月27日 19:35:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定