How to get that @Indexed (unique = true) failed

I use Spring-Boot 1.5.1andMongoDB 3.4.6

I have a document MongoDBthat contains some annotation @Indexed(unique=true)in some field.

@Document(collection="Product")
public class Product{
    @Id
    private String id;
    @Indexed(unique=true)
    private String name;
    @Indexed(unique=true)
    private String searchName;

If there is a duplicate name or searchName, it returns org.springframework.dao.DuplicateKeyException.

Stacktrace:

Caused by: org.springframework.dao.DuplicateKeyException: E11000 duplicate key error index: Product.name  dup key: { : "name" }; nested exception is com.mongodb.MongoException$DuplicateKey: E11000 duplicate key error index: Product.name dup key: { : "name" }
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:52)

How can we get the key to which the exception was sent.

Something like when we put @NotNull(message = "Product briefDescription cannot be null")in some kind of folder, and it gives messagein exception, but the attribute messagefor annotation is @Indexedmissing.

How to do it?

+4
source share
3 answers

, , , ( Product.name Product.searchName).

, . :

" "

JIRA-:

, ( , ), , .

+5

, , -. , . Mongo Jira, , .

0

, . , ., :

db.collection('system.indexes').findOne({ ns: 'mean-dev.users', name: 'username_1' }, cb);
0

Source: https://habr.com/ru/post/1686283/


All Articles